Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6081869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:13:28+00:00 2026-05-23T11:13:28+00:00

This is my XML file. <body> <div> <p time=00:00:08> </p> <p time=00:00:10> </p> <p

  • 0

This is my XML file.

<body>
   <div>
     <p time="00:00:08"> </p>
     <p time="00:00:10"> </p>
     <p time="00:00:13"> </p>
   </div>
</body>

Now I want to add time = “00:00:12” to the XML file, but in increasing order.
So, before adding this time, I will have to compare the time with other times and then add it at appropriate location.

Can anybody suggest me how to do this. A sample code would very helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:13:29+00:00Added an answer on May 23, 2026 at 11:13 am

    As was suggested in Gordon’s answer, I would load the XML file into SimpleXML, append the node and then sort it.

    I’ve commented the code below to explain it step-by-step.

    <?php
    
    // Open the XML file
    $xml = file_get_contents("captions.xml");
    // SimpleXml is an "easy" API to manipulate XML
    // A SimpleXmlElement is any element, in this case it will be the <body> 
    // element as it is first in the file.
    $timestamps = new SimpleXmlElement($xml);
    
    // Add our new time entry to the <div> element inside <body>
    $timestamps->div->addChild("p", null);
    // Get the index of the last element (the one we just added)
    $index = $timestamps->div->p->count()-1;
    // Add a time attribute to the element we just added
    $e = $timestamps->div->p[$index];
    $e->addAttribute("time", "00:00:12");
    // Replace it with the new one (with a time attribute)
    $timestamps->div->p[$index] = $e;
    
    // Sort the elements by time (I've used bubble sort here as it's in the top of my head)
    // Make sure you're setting this here or in php.ini, otherwise we get lots of warnings :)
    date_default_timezone_set("Europe/London");
    
    /**
     * The trick here is that SimpleXmlElement returns references for nearly
     * everything. This means that if you say $c = $timestamps->div->p[0], changes
     * you make to $c are made to $timestamps->div->p[0]. It's the same as calling
     * $c =& $timestamps->div->p[0]. We use the keyword clone to avoid this.
     */ 
    $dates = $timestamps->div->children();
    $swapped = true;
    while ($swapped) {
        $swapped = false;
        for ($i = 0; $i < $dates->count() - 1; $i++) {
            $curTime = clone $dates[$i]->attributes()->time;
            $nextTime = clone $dates[$i+1]->attributes()->time;
    
            // Swap if current is later than next
            if (strtotime($curTime) > strtotime($nextTime)) {
                $dates[$i]->attributes()->time = $nextTime;
                $dates[$i+1]->attributes()->time = $curTime;
                $swapped = true;
                break;
            }
        }
    }
    
    // Write back
    echo $timestamps->asXml();
    //$timestamps->asXml("captions.xml");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With this XML file - <Instructions> <Admin> <Instruction> Steps must be completed in order.&lt;/br&gt;
This is my Xml file.i want to transform this xml file into another customized
I have thread where downloading xml file a this xml file i want to
Hi I can't understand this error I want read this XML file: <?xml version=1.0
I have this xml file which has text init. i.e Hi my name is
I got this XML file: <Msg UserText=start 0> </Msg> <Msg UserText=A> </Msg> <Msg UserText=A>
I have this xml file <?xml version=1.0 encoding=UTF-8?> <bo:C837ClaimParent xsi:type=bo:C837ClaimParent xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:bo=http://somelongpathHere/process/bo> <claimAux> ...
I am receiving this XML file which I would like to validate. Ideally I
I am using an XML file to store the values. This XML file can
Here is my ultimate goal... to take this xml file.. <?xml version=1.0?> <Songs> <Song>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.