Over the last few working days, I spent a quite a bit of time playing around with XML. While searching through the net, I found few comprehensive PHP XML guides. There never was a ‘1 stop all operations’ guide for learning XML.
As such I decided to club together examples of all kinds of operations I ever did on XML in a single post. I hope it benefits others out there who wish to learn more about XML manipulation.
Note : Since the post got quite large, I decided to only use the Tree Map style parsers – DOM & Simple XML.

XML Pic

Operations Performed:
(1) Create XML OR Array to XML Conversion OR CDATA Element Eg
(2) Edit XML – Edit/Modify Element Data (accessed serially)
(3) Edit XML – Edit specific Elements (accessed conditionally)
(4) Edit XML – Element Addition (to queue end)
(5) Edit XML – Element Addition (to queue start)
(6) Edit XML – Element Addition (before a specific node)
(7) Delete Elements (accessed serially)
(8) Delete Elements (accessed conditionally)
(9) Rearrange / Reorder Elements
(10) Display Required data in XML Form itself OR Remove all children nodes save one OR Copy/Clone Node Eg OR Compare/Search non numeric data (like date or time) to get result.

library.xml will be used in all operations.
ps : I have added the indention & spaces outside the tags in the below xml for a presentable xml form.
Remove them before saving your xml file else most of the usual XML functions wont work in the desired manner.

<?xml version="1.0"?>
<library>
    <book isbn="1001" pubdate="1943-01-01">
        <title><![CDATA[The Fountainhead]]></title>
        <author>Ayn Rand</author>
        <price>300</price>
    </book>
    <book isbn="1002" pubdate="1954-01-01">
        <title><![CDATA[The Lord of the Rings]]></title>
        <author>J.R.R.Tolkein</author>
        <price>500</price>
    </book>
    <book isbn="1003" pubdate="1982-01-01">
        <title><![CDATA[The Dark Tower]]></title>
        <author>Stephen King</author>
        <price>200</price>
    </book>
</library>

#######################################
// (1) Create XML OR
Array to XML Conversion OR
CDATA Element Eg
#######################################

// (i) SimpleXML :

// Cant create CDATA element for title in SimpleXML.
function fnSimpleXMLCreate()
    {
        $arr = array(array('isbn'=>'1001', 'pubdate'=>'1943-01-01', 'title'=>'The Fountainhead',
                               'author'=>'Ayn Rand', 'price'=>'300'),
                         array('isbn'=>'1002', 'pubdate'=>'1954-01-01',
                               'title'=>'The Lord of the Rings', 'author'=>'J.R.R.Tolkein',
                               'price'=>'500'),
                         array('isbn'=>'1003', 'pubdate'=>'1982-01-01', 'title'=>'The Dark Tower',
                               'author'=>'Stephen King', 'price'=>'200'));
        
        $library = new SimpleXMLElement('<library />');
        
        for($i=0;$i<3;$i++)
        {
            $book = $library->addChild('book');
            $book->addAttribute('isbn', $arr[$i]['isbn']);
            $book->addAttribute('pubdate', $arr[$i]['pubdate']);
            $book->addChild('title', $arr[$i]['title']); //cant create CDATA in SimpleXML.
            $book->addChild('author', $arr[$i]['author']);
            $book->addChild('price', $arr[$i]['price']);
        }
       
        $library->asXML('library.xml');
    }


// (ii) DOM :

function fnDomCreate()
    {
       $arr = array(array('isbn'=>'1001', 'pubdate'=>'1943-01-01', 'title'=>'The Fountainhead',
                               'author'=>'Ayn Rand', 'price'=>'300'),
                         array('isbn'=>'1002', 'pubdate'=>'1954-01-01',
                               'title'=>'The Lord of the Rings', 'author'=>'J.R.R.Tolkein',
                               'price'=>'500'),
                         array('isbn'=>'1003', 'pubdate'=>'1982-01-01', 'title'=>'The Dark Tower',
                               'author'=>'Stephen King', 'price'=>'200'));
        
        $dom = new DOMDocument();
        $library = $dom->createElement('library');
        $dom->appendChild($library);
        
        for($i=0;$i<3;$i++)
        {
            $book = $dom->createElement('book');
            $book->setAttribute('isbn',$arr[$i]['isbn']);
             $book->setAttribute('pubdate',$arr[$i]['pubdate']);
            
            //$prop = $dom->createElement('title', $arr[$i]['title']);
            $prop = $dom->createElement('title');
            $text = $dom->createCDATASection($arr[$i]['title']);
            $prop->appendChild($text);
            $book->appendChild($prop);
            
            $prop = $dom->createElement('author', $arr[$i]['author']);
            $book->appendChild($prop);
            $prop = $dom->createElement('price', $arr[$i]['price']);
            $book->appendChild($prop);
            $library->appendChild($book);
        }
        //header("Content-type: text/xml");
        $dom->save('library.xml');
    }

#######################################
// (2) Edit XML – Edit/Modify Element Data (accessed serially)
#######################################

// (i) SimpleXML :

// Edit Last Book Title
function fnSimpleXMLEditElementSeq()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $num = count($library);
        $library->book[$num-1]->title .= ' - The Gunslinger';
        header("Content-type: text/xml");
        echo $library->asXML();
    }


// (ii) DOM :

//Edit Last Book Title
    function fnDOMEditElementSeq()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $cnt = $library->childNodes->length;
        
        $library->childNodes->item($cnt-1)->getElementsByTagName('title')->item(0)->nodeValue .= ' Series'; 
       // 2nd way #$library->getElementsByTagName('book')->item($cnt-1)->getElementsByTagName('title')->item(0)->nodeValue .= ' Series';
       
       //3rd Way
       //$library->childNodes->item($cnt-1)->childNodes->item(0)->nodeValue .= ' Series';
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (3) Edit XML – Edit specific Elements (accessed conditionally)
#######################################

// (i) SimpleXML :

//Edit Title of book with author J.R.R.Tolkein
    function fnSimpleXMLEditElementCond()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->xpath('/library/book[author="J.R.R.Tolkein"]');
        $book[0]->title .= ' Series';
        header("Content-type: text/xml");
        echo $library->asXML();
    }


// (ii) DOM (with XPath):

 //Edit Title of book with author J.R.R.Tolkein
    function fnDOMEditElementCond()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $xpath = new DOMXPath($dom);
        $result = $xpath->query('/library/book[author="J.R.R.Tolkein"]/title');
        $result->item(0)->nodeValue .= ' Series';
        // This will remove the CDATA property of the element.
        //To retain it, delete this element (see delete eg) & recreate it with CDATA (see create xml eg).
        
        //2nd Way
        //$result = $xpath->query('/library/book[author="J.R.R.Tolkein"]');
       // $result->item(0)->getElementsByTagName('title')->item(0)->nodeValue .= ' Series';
        header("Content-type: text/xml");
        echo $dom->saveXML();
        
    }

#######################################
// (4) Edit XML – Element Addition (to queue end)
#######################################

// (i) SimpleXML :

//Add another Book to the end
    function fnSimpleXMLAddElement2End()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->addChild('book');
        $book->addAttribute('isbn', '1004');
        $book->addAttribute('pubdate', '1960-07-11');
        $book->addChild('title', "To Kill a Mockingbird");
        $book->addChild('author', "Harper Lee");
        $book->addChild('price', "100");
        header("Content-type: text/xml");
        echo $library->asXML();
    }


// (ii) DOM :

    //Add another Book to the end
    function fnDOMAddElement2End()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        
        $book = $dom->createElement('book');
        $book->setAttribute('isbn','1000');
        $book->setAttribute('pubdate','1960-07-11');
        
        $prop = $dom->createElement('title');
        $text = $dom->createTextNode('To Kill a Mockingbird');
        $prop->appendChild($text);
        $book->appendChild($prop);
        
         $prop = $dom->createElement('author','Harper Lee');
        $book->appendChild($prop);
        $prop = $dom->createElement('price','100');
        $book->appendChild($prop);
        
        $library->appendChild($book);
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
//(5) Edit XML – Element Addition (to queue start)
#######################################

// (i) SimpleXML :

// Add a Book to List Start
// Insert Before Functionality not present in SimpleXML 
// We can integrate DOM with SimpleXML to do it.
    function fnSimpleXMLAddElement2Start()
    {
        $libSimple = new SimpleXMLElement('library.xml',null,true);
        $libDom = dom_import_simplexml($libSimple);
        
        $dom = new DOMDocument();
        //returns a copy of the node to import
        $libDom = $dom->importNode($libDom, true);
        //associate it with the current document.
        $dom->appendChild($libDom);
        
        fnDOMAddElement2Start($dom); //see below DOM function
    }


// (ii) DOM :

function fnDOMAddElement2Start($dom='')
    {
        if(!$dom)
        {
            $dom = new DOMDocument();
            $dom->load('library.xml');
        }
        $library = $dom->documentElement;
        #var_dump($library->childNodes->item(0)->parentNode->nodeName);
        $book = $dom->createElement('book');
        $book->setAttribute('isbn','1000');
        $book->setAttribute('pubdate','1960-07-11');
         
        $prop = $dom->createElement('title','To Kill a Mockingbird');
        $book->appendChild($prop);
         $prop = $dom->createElement('author','Harper Lee');
        $book->appendChild($prop);
         $prop = $dom->createElement('price','100');
        $book->appendChild($prop);
        
        $library->childNodes->item(0)->parentNode->insertBefore($book,$library->childNodes->item(0));
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (6) Edit XML – Element Addition (before a specific node)
#######################################

// (i) SimpleXML :

// Add a Book Before attribute isbn 1002
    // Insert Before Functionality not present in SimpleXML 
    // We can integrate DOM with SimpleXML to do it.
    function fnSimpleXMLAddElementCond()
    {
        $libSimple = new SimpleXMLElement('library.xml',null,true);
        $libDom = dom_import_simplexml($libSimple);
        
        $dom = new DOMDocument();
        //returns a copy of the node to import
        $libDom = $dom->importNode($libDom, true);
        //associate it with the current document.
        $dom->appendChild($libDom);
        
        fnDOMAddElementCond($dom); //see below DOM eg.
    }


// (ii) DOM :

// Add a Book Before isbn 1002
    function fnDOMAddElementCond($dom='')
    {
        if(!$dom)
        {
            $dom = new DOMDocument();
            $dom->load('library.xml');
        }
        $library = $dom->documentElement;
        
        $book = $dom->createElement('book');
        $book->setAttribute('isbn','1000');
        $book->setAttribute('pubdate', '1960-07-11');
        
        $prop = $dom->createElement('title','To Kill a Mockingbird');
        $book->appendChild($prop);
         $prop = $dom->createElement('author','Harper Lee');
        $book->appendChild($prop);
        $prop = $dom->createElement('price','100');
        $book->appendChild($prop);
        
        $xpath = new DOMXPath($dom);
        $result = $xpath->query('/library/book[@isbn="1002"]');
        $library->childNodes->item(0)->parentNode->insertBefore($book,$result->item(0));
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (7) Delete Elements (accessed serially)
#######################################

// (i) SimpleXML :

// Delete 2nd book
    function fnSimpleXMLDeleteSeq()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        //$library->book[1] = null; // this only empties content
        unset($library->book[1]);
        header("Content-type: text/xml");
        echo $library->asXML();
        
    }


// (ii) DOM :

// Delete 2nd Book
    function fnDOMDeleteSeq()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        
        $library->childNodes->item(0)->parentNode->removeChild($library->childNodes->item(1));
        
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (8) Delete Elements (accessed conditionally)
#######################################

// (i) SimpleXML :

// Delete a book with  200<price<500
    // Not possible to delete node found via XPath in SimpleXML. See below.
    function fnSimpleXMLDeleteCond()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->xpath('/library/book[price>"200" and price<"500"]');
        
        //Problem here....not able to delete parent node using unset($book[0]);
        // unset of parent node only works when accessing serially. eg : unset($library->book[0]);
        
        //header("Content-type: text/xml");
        //echo $library->asXML();
        
    }


// (ii) DOM :

// Delete the book with  200<price<500 
    function fnDOMDeleteCond()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $xpath = new DOMXPath($dom);
        $result = $xpath->query('/library/book[price>"200" and price<"500"]');
        $result->item(0)->parentNode->removeChild($result->item(0));
        header("Content-type: text/xml");
        echo $dom->saveXML();
    }

#######################################
// (9) Rearrange / Reorder Elements
#######################################

// (i) SimpleXML :

// Exchange Position of 2nd book with 3rd : fnSimpleXMLRearrange(2,3);
// Due to absence of an inbuilt function (DOM has it), we have to make our own function in SimpleXML.
//Better to use DOM.
function fnSimpleXMLRearrange($num1,$num2)
{
     $libSimple= new SimpleXMLElement('library.xml',null,true);
     //$library->book[3] = $library->book[0]; // this doesnt work
 
    $libDom = dom_import_simplexml($libSimple);

    $dom = new DOMDocument();
    //returns a copy of the node to import
    $libDom = $dom->importNode($libDom, true);
    //associate it with the current document.
    $dom->appendChild($libDom);

    fnDOMRearrange($num1,$num2,$dom); // see below DOM function
}


// (ii) DOM :

// Exchange Position of 2nd book with 3rd : fnDOMRearrange(2,3);
function fnDOMRearrange($num1,$num2,$dom=0)
{
    if(!$dom)
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
    }
    $dom = new DOMDocument();
    $dom->load('library.xml');
    $library = $dom->documentElement;
	$cnt = $library->childNodes->length;
	//echo '<br> count : ' . $cnt;

    if ($num1 > $num2) { // ensure $num1 always is less than $num2
        $num3 = $num1;
        $num1 = $num2;
        $num2 = $num3;						
    }
     
    $index1 = fnDOMConvIndex($num1,  $cnt);
    $index2 = fnDOMConvIndex($num2,  $cnt);
    /*for ($iter = 0; $iter <= $cnt; $iter++) {
        echo '<hr>' . $iter . '<br>' .  $library->childNodes->item($iter)->nodeValue;
    }*/
            
    $library->childNodes->item(0)->parentNode->insertBefore($library->childNodes->item($index1), $library->childNodes->item($index2)); // put x before y
    $library->childNodes->item(0)->parentNode->insertBefore($library->childNodes->item($index2), $library->childNodes->item($index1)); // put y before x 

    header("Content-type: text/xml");
    echo $dom->saveXML();
}
function fnDOMConvIndex($num,  $cnt)
{
    // echo '<br>' . $num;
    if ($num != 1) {
        $maxIndex = floor($cnt/2);
        //echo '<br>max: ' . $maxIndex;
        if ($maxIndex == $num) {
            $num = $cnt-2;
        } else {
            $num = ($num%2)?($num+2):($num+1); //Always odd index due to nature of DOM Element Index.
        }
    }         
    
    // echo '<br>' . $num;
    return $num;
}

#######################################
// (10) Display Required data in XML Form itself OR
Remove all children nodes save one OR
Copy/Clone Node Eg OR
Compare/Search non numeric data (like date or time) to get result.
#######################################

// (i) SimpleXML :

// Display Books published after 1980 in XML Form itself.
// No function to copy node directly in SimpleXML.
// Its simpler for this functionality to be implemented in DOM.
    function fnSimpleXMLDisplayElementCond()
    {
        $library = new SimpleXMLElement('library.xml',null,true);
        $book = $library->xpath('/library/book[translate(@pubdate,"-","")>translate("1980-01-01","-","")]');
        // Manually create a new structure then add searched data to it (see create xml eg.)
    }


// (ii) DOM :

// Display Books published after 1980 in XML Form itself.
    function fnDOMDisplayElementCond()
    {
        $dom = new DOMDocument();
        $dom->load('library.xml');
        $library = $dom->documentElement;
        $xpath = new DOMXPath($dom);
        
        // Comparing non numeric standard data
        $result = $xpath->query('/library/book[translate(@pubdate,"-","")>translate("1980-01-01","-","")]');
        // For simpler search paramater use this :
        //$result = $xpath->query('/library/book[author="J.R.R.Tolkein"]');
        
        // Copy only node & its attributes not its contents.
        $library = $library->cloneNode(false);
        // Add the 1 element which is search result.
        $library->appendChild($result->item(0));
        
        header("Content-type: text/xml");
        echo $dom->saveXML($library);
    }

Lessons Learn’t :

  • SimpleXML is fantastic for those who will only briefly flirt with XML (or beginners) & perform simple operations on XML.
  • DOM is an absolute necessity for performing complex operations on XML data. Its learning curve is higher than SimpleXML off course but once you get the hang of it , you will know its very logical.
  • Use XPath for conditional access to data. For serial access (like last book) XPath is not needed (but u can use it) since I can use normal DOM / SimpleXML node access.

Ref :
IBM : XML Guide for PHP Developers – Part 2
http://www.devx.com/webdev/Article/41975/1954


Advertisements

42 thoughts on “PHP : XML Create-Add-Edit-Modify using DOM , SimpleXML , XPath

  1. Example 9 doesn’t work for me, it outputs:

    Ayn Rand
    300

    J.R.R.Tolkein
    500

    Stephen King
    200

    It appears that the changes aren’t updated.

    1. Good Catch.
      DOM Code Rectified so as to reflect appropriate XML indices.
      SimpleXML code changed so as to depend on DOM now since earlier code wouldn’t work for CDATA section.

      1. Thanks this works perfectly, where did you find the information about DOM’s XML indices? I would like to read up on that.

        I’ve been looking to do reordering of XML elements on and off for 4 years now. Thanks again!

  2. Discovered it quite a while back when I was practicing with XML DOM. Index 0 is tied to a blank text node while Index 1 points to the XML node (1st book) & so on alternatively. Had forgotten abt it since SimpleXML doesn’t have it.

    1. You are welcome. I can understand your situation…went through the same last year…hence decided to do something about it. Glad it was of use to you 🙂

  3. Great examples ! That’s what I was looking for. Just one thing I’m struggling with, maybe you can help. My xml structure is hierarchical :
    http://pastebin.com/zGKShpgS .
    I need to find element by id so i use xpath->query(‘//item[@id=x]) , but now how can I appendChild after or before this element ? I’d thought it would be as simple as $library->insertBefore($newitem,$xpathresult), but it doesn’t seem to work. Any ideas ?

  4. Hi there I got question:

    I am trying to edit below table.xml file using php base on the result.xml but your solution did not work for me. can you help me please.

    table.xml

    Man U
    1
    0
    0
    1
    0
    0

    Chelsea
    0
    0
    0
    0
    0
    o

    LiverPool
    1
    1
    0
    0
    0
    3

    Arsenal
    1
    0
    1
    0
    0
    1

    and result.xml

    Results

    Man u
    6

    Chelsea
    0

    .
    .
    .

    thanks

  5. Here is the details what exactly I am trying to do:

    read the result.xml file
    then compare with team name with table.xml
    if team name match with result.xml

    update record in table.xml
    for home team
    update no of match played +1
    update points on the base of goal (6-0)

    for away team
    update no of match played +1
    update points on the base of goal (0-6)

    hope you got it now

    1. Not sure if I understood your xml structure…cause the numbers don’t make sense…do they represent goals scored or points achieved ? why don’t you mention the proper xml with the node names instead of just the values.
      As far as I can understand, you plan to use the result.xml file to update the table.xml file using the club name as the search parameter. The “(3) Edit XML – Edit specific Elements (accessed conditionally)” eg should help you to find the club name (assuming its an attribute value).

  6. here is my xml file
    table.xml

    Man U
    1
    0
    0
    1
    0
    0

    Chelsea
    0
    0
    0
    0
    0
    o

    LiverPool
    1
    1
    0
    0
    0
    3

    Arsenal
    1
    0
    1
    0
    0
    1

    result.xml

    Man U
    6

    Chelsea
    0

    .
    .
    .
    .
    .
    .

    hope you will be able to help.

  7. result.xml file:

    
    <?xml version="1.0"?>
                          <results>
                                   <result id="6548">
                                           <hometeam>
                                                     <name>Man U</name>
                                                     <score>6</score>
                                           </hometeam>
                                           <awayteam>
                                                     <name>Chelsea</name>
                                                     <score>0</score>
                                           </awayteam>
                                   </result>
                                   <result id="6548">
                                   .
                                   .
                                   .
                                   .
                                   .
                                   .
                                    </result>
    
                          </results>
    

    table.xml

    <?xml version="1.0" encoding="iso-8859-1"?>
    <Teams>
      <Team>
        <name>Man U</name>
        <Played>1</Played>
        <Won>0</Won>
        <Drawn>0</Drawn>
        <Lost>1</Lost>
        <GoalDif>0</GoalDif>
        <Points>0</Points>
      </Team>
      <Team>
        <name>Chelsea</name>
        <Played>0</Played>
        <Won>0</Won>
        <Drawn>0</Drawn>
        <Lost>0</Lost>
        <GoalDif>0</GoalDif>
        <Points>o</Points>
      </Team>
      <Team>
        <name>LiverPool</name>
        <Played>1</Played>
        <Won>1</Won>
        <Drawn>0</Drawn>
        <Lost>0</Lost>
        <GoalDif>0</GoalDif>
        <Points>3</Points>
      </Team>
      <Team>
        <name>Arsenal</name>
        <Played>1</Played>
        <Won>0</Won>
        <Drawn>1</Drawn>
        <Lost>0</Lost>
        <GoalDif>0</GoalDif>
        <Points>1</Points>
      </Team>
    </Teams>
    

    hope you will be able to help.

    1. Sorry I couldn’t reply earlier….bunch of stuff kept me busy.
      Built a sample solution in which we read from result.xml & edit the played element of table.xml :

      function fnDOMEditElementSeq()
      {
          
          $domTable = new DOMDocument();
          $domTable->load('table.xml');    
          $xpath = new DOMXPath($domTable);
      
          $domResult = new DOMDocument();
          $domResult->load('result.xml');
          
          $results = $domResult->documentElement;
          $cnt = $results->childNodes->length;
          for($iter = 0; $iter < $cnt; $iter++) {
              $result = $results->getElementsByTagName('result')->item($iter);
              $teamName = $result->getElementsByTagName('hometeam')->item(0)->getElementsByTagName('name')->item(0)->nodeValue;
              if ($teamName) {
                  $searchResult = $xpath->query('/Teams/Team[name="' .$teamName.'"]');
                  $searchResult->item(0)->getElementsByTagName('Played')->item(0)->nodeValue += 1;            
              }        
          }    
          // To display the xml;
          //header("Content-type: text/xml");
          //echo $domTable->saveXML();
          
          $domTable->save('table.xml');
      }
      
      1. Thank you very much for your help, but function did not work with me, sorry to bother you again, have you tested the function.? Hope you will get back to me when you got time. thanks.

      2. Here is the error:
        Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\dev\www\read.php on line 17

        
        if ($teamName) {
                    $searchResult = $xpath->query('/Teams/Team[name="' .$teamName.'"]');
                    $searchResult->item(0)->getElementsByTagName('Played')->item(0)->nodeValue += 1;
                }
        

        which is pointing last line of if statement, can you please have a look. thanks

      3. $domTable = new DOMDocument();
            $domTable->load('LeagueTable.xml');
            $xpath = new DOMXPath($domTable);
        
            $domResult = new DOMDocument();
            $domResult->load('result.xml');
        
            $results = $domResult->documentElement;
            $cnt = $results->childNodes->length;
            $homeTeamName ='';
            $awayTeamName ='';
            $homeScore =0;
            $AwayScore =0;
            $homePoints =0;
            $awayPoints =0;
            $homeWon =0;
            $awayWon =0;
            $homeLost =0;
            $awayLost =0;
            $howDrawn =0;
            $awayDrawn =0;
        
            for($iter = 0; $iter < $cnt; $iter++) {
                $result = $results->getElementsByTagName('result')->item($iter);
                $homeTeamName = $results->getElementsByTagName('hometeam')->item(0)->getElementsByTagName('name')->item(0)->nodeValue;
                $homeScore = $results->getElementsByTagName('hometeam')->item(0)->getElementsByTagName('score')->item(0)->nodeValue;
                $awayTeamName = $results->getElementsByTagName('awayteam')->item(0)->getElementsByTagName('name')->item(0)->nodeValue;
                $AwayScore = $results->getElementsByTagName('awayteam')->item(0)->getElementsByTagName('score')->item(0)->nodeValue;
        
            }
                if($homeScore - $AwayScore >=1)
                {
                   $homePoints =3;
                   $awayPoints =0;
                   $homeWon =1;
                   $awayLost =1;
        
        
                }
                elseif($homeScore - $AwayScore =0)
                {
                   $homePoints =1;
                   $awayPoints =1;
                   $howDrawn =1;
                   $awayDrawn =1;
                }
                 else
                 {
                    $homePoints =0;
                    $awayPoints =3;
                    $homeLost =1;
                    $awayWon =1;
                 }
        
                if ($homeTeamName) {
                    $searchResult = $xpath->query('/Teams/Team[Name="' .$homeTeamName.'"]');
                    $searchResult->item(0)->getElementsByTagName('Played')->item(0)->nodeValue += 1;
                    $searchResult->item(0)->getElementsByTagName('Points')->item(0)->nodeValue += $homePoints;
                    $searchResult->item(0)->getElementsByTagName('GoalDif')->item(0)->nodeValue += $homeScore - $AwayScore;
                    $searchResult->item(0)->getElementsByTagName('Won')->item(0)->nodeValue += $homeWon;
                    $searchResult->item(0)->getElementsByTagName('Lost')->item(0)->nodeValue += $homeLost;
                    $searchResult->item(0)->getElementsByTagName('Drawn')->item(0)->nodeValue += $homeDrawn;
        
                }
                if ($awayTeamName) {
                    $searchResult = $xpath->query('/Teams/Team[Name="' .$awayTeamName.'"]');
                    $searchResult->item(0)->getElementsByTagName('Played')->item(0)->nodeValue += 1;
                    $searchResult->item(0)->getElementsByTagName('Points')->item(0)->nodeValue += $awayPoints;
                    $searchResult->item(0)->getElementsByTagName('GoalDif')->item(0)->nodeValue += $AwayScore - $homeScore;
                     $searchResult->item(0)->getElementsByTagName('Won')->item(0)->nodeValue += $awayWon;
                    $searchResult->item(0)->getElementsByTagName('Lost')->item(0)->nodeValue += $awayLost;
                    $searchResult->item(0)->getElementsByTagName('Drawn')->item(0)->nodeValue += $awayDrawn;
        
                }
        
            //To display the xml;
            header("Content-type: text/xml");
            echo $domTable->saveXML();
        
            $domTable->save('LeagueTable.xml');
        
        
        ?>
        

        hi there, i have modified your code and end the for loop before if statement becouse loop goes round 3 time with the tags (, and ) as result played add up 3 times. it work fine but i cant read more then one result, can you please have a look the code i have modified?? thank you in advance.

    1. Hi, I am looking for something similar, its part of my assignment in college. Could you please help me if possible. It would be great help. I would immensely appreciate if you could mail it to me. My email id is sanjyot_31@yahoo.com

  8. hi there, i was try to learn about php and xml but when i tried to cek, update, and create i got stuck, maybe you can help me out with this snippet

    load(‘server.xml’);
    $dashboard_xpath = new DOMXPath($dashboard_xml);
    $dashboard_query = $dashboard_xpath->query(‘/Dashboard/Server[@id=1]’);
    $dashboard_result = false;
    foreach($dashboard_query as $dashboard_result){
    if($dashboard_result){
    // Update XML FIle Elements;
    } else {
    // Create XML Elements;
    }
    }
    ?>

    172.17.2.71
    80
    3306

    172.17.2.71
    21
    80

    i was googling all day long but still nothing, wish you can help me out with a bit example or direct we to a good tuts web, really thanks before.

  9. Hi,
    dom_import_simplexml is not working when i tried for Reorder of elements in XML.

    I am getting empty array.

    Let me know what will be the problem.

    $libSimple= new SimpleXMLElement(‘library.xml’,null,true); // getting Simplexmlelement
    $libDom = dom_import_simplexml($libSimple); // Empty array is the output for this line.

    Thanks,
    Prakash Mukta

    1. dom_import_simplexml is an inbuilt function.
      It will always work provided the xml file is proper.

      Btw thx to your comment, I just checked the code & found an issue in the reordering of elements when the elements are not alongside each other.
      The issue is fixed now !

  10. Good afternoon,
    How would I do if my xml was cascading?

    eg

    
    
      
        
        Ayn Rand
        300
    		  
    			
    			J.R.R.Tolkein
    			500
    				  
    					
    					Stephen King
    					200
    
    					
    						//1004A - NEW 
    						  
    							
    							Stephen King
    							200
    						  
    						  
    						//1004B - OLD 
    						  
    							
    							Stephen King
    							200
    						  
    				  
    		  
      
    
    
  11. Hello, I am trying to append an XML file and i should save new elements to the xml file, information taken from textboxes. But unfortunately after trying some of your codes i am unable to write the my xml file.

    Could you please help.

  12. Hi Thank you for this tutorial, soo so helpful,
    The code is working fine on the browser but not saving the changes to the file and no thing change in the xml file?

  13. (2) Edit XML – Edit/Modify Element Data (accessed serially)
    part (ii) DOM – not working for me.

    $cnt = $library->childNodes->length; // $cnt return 7 !??? not 2 – it is 3 books
    $library->childNodes->item($cnt-1)->getElementsByTagName(‘title’)->item(0)->nodeValue .= ‘ Series’;
    == Fatal error: Call to undefined method DOMText::getElementsByTagName() …

  14. foreach($library->childNodes as $node){
    echo $node->nodeName.’ – ‘.$node->nodeValue, “”;
    }
    // it is list 7 childrens & values 😦
    /*
    #text –
    book – The Fountainhead Ayn Rand 300
    #text –
    book – The Lord of the Rings J.R.R.Tolkein 500
    #text –
    book – The Dark Tower Stephen King 200
    #text –

  15. Hello congratulations for your article.
    I used the codes, I created a file library.xml and lib.php where I copied the inside:
    // (I) SimpleXML
    // Edit Last Book Title
    fnSimpleXMLEditElementSeq function ()
         {
             $ library = new SimpleXMLElement (‘library.xml’, null, true);
             $ num = count ($ library);
             $ library-> book [$ num-1] -> title. = ‘- The Gunslinger’;
             header (“Content-type: text / xml”);
             echo $ library-> asXml ();
         }

    But unfortunately it does not work, where am I wrong? as the function is called or what parameters I have to pass?

    Thank you and congratulations for the work done.

  16. I think for append record before you have wrong code as you have written

    header(“Content-type: text/xml”);
    echo $dom->saveXML();

    Instead of
    echo $dom->saveXML(); it should be
    echo $dom->save(‘library.xml’);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s