0

I have XML file as below:

<?xml version="1.0" encoding="UTF-8"?>
<root version="8.0.0.0">
<songs name="Album">
  <row>
    <song artist="artist_name">Track1</song>
  </row>
  <row>
    <song artist="artist_name">Track2</song>
  </row>
  <row>
    <song artist="artist_name">Track3</song>
  </row>
  <row>
    <song artist="artist_name">Track4</song>
  </row>
</songs>
</root>

Now i want to update this file with some more rows. How i can append data on top of the existing row elements? Also while adding new elements i want to check the tracks like - Track1, Track2 are not duplicates.

Currently i'm manipulating this xml file with php:dom, but its appending data at the bottom of the existing rows.

PHP code used to do above things is

<?php
 //Creates XML string and XML document using the DOM 
 $dom = new DOMDocument(); 
 $dom->formatOutput = true;
 $dom->Load('C:/wamp/www/xml/test1.xml');

 $root = $dom->firstChild;
 $list = $root->childNodes->item(1);


if(isset($_POST['submit'])){

    $artistName = $_POST['name'];
    $track = $_POST['track'];
    $row = $dom->createElement('row');
    $list->appendChild($row);
    $song = $dom->createElement('song'); 
    $row->appendChild($song);
    $song->setAttribute('artist', $artistName);
    $wcm_node = $dom->createTextNode($track);
    $song->appendChild($wcm_node);
 }

// Code to format XML after appending data
$outXML = $dom->saveXML(); // put string in outXML 

//now create a brand new XML document
$xml = new DOMDocument(); 
$xml->preserveWhiteSpace = false; 
$xml->formatOutput = true; //yup, going to try to make this format again
//pass the output of the first bunch of stuff we did to the new XML document:
$xml->loadXML($outXML); 
//now display the cleanly formatted output without using LIBXML_NOBLANKS (which may have undesired consequences
$xml->save('test1.xml'); // save as file 

}

?>

Please let me know, how i can do it.

Thanks

1

1 Answer 1

2

That's not appending but prepending. DOM has a method for that, too:

Example (demo):

$dom = new DOMDocument;
$dom->loadXml('<rows><row xml:id="r1"/></rows>');
$dom->documentElement->insertBefore(
    $dom->createElement('row', 'new row'),
    $dom->getElementById('r1')
);
$dom->formatOutput = TRUE;
echo $dom->saveXml();

Output:

<?xml version="1.0"?>
<rows>
  <row>new row</row>
  <row xml:id="r1"/>
</rows>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Gordon.. its not working since i dont have any id in that xml to find and append? is there anything else i can do to append the new elements right after the <songs name="Album">??
@techiepark your concrete markup is irrelevant. If you look at the linked manual you will see that is says "reference node", so you just pass in the node you want the new element to be inserted before. How you get hold of that node is up to you. Any method that returns a DOMElement from the DOMDocument will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.