Adding data to xml
Posted: Mon Jun 29, 2009 9:38 am
I'm trying to write new items to my xml, I want the new items to be written as the first item in my xml and the old below. I read that I should read in the old first and then save a new version with the new item on top but I can't understand how..?
So my old xml is like this:
My code for adding items:
And this Is how I want my new XML to look like:
So my old xml is like this:
Code: Select all
<videos>
<item>
<question>Old question</question>
<answers>Old answers</answers>
<url>Old url</url>
<position>Old pos</position>
<country>Old Country</country>
<category>Old category</category>
</item>
<item>Code: Select all
$xdoc = new DomDocument;
$xdoc->Load($xmlPath);
$videos = $xdoc->getElementsByTagName('videos')->item(0);
$addItem = $xdoc ->createElement('item');
$addQuestion = $xdoc ->createElement('question', $Question);
$addAnswers = $xdoc ->createElement('answers', $Answers);
$addURL = $xdoc ->createElement('url', $umaURL);
$addPosition = $xdoc ->createElement('position', $Position);
$addCountry = $xdoc ->createElement('country', $country);
$addCategory = $xdoc ->createElement('category', $category);
$videos -> appendChild($addItem);
$addItem -> appendChild($addQuestion);
$addItem -> appendChild($addAnswers);
$addItem -> appendChild($addURL);
$addItem -> appendChild($addPosition);
$addItem -> appendChild($addCountry);
$addItem -> appendChild($addCategory);
$saveXML = $xdoc->save($xmlPath);
Code: Select all
<videos>
<item>
<question>New question</question>
<answers>New answers</answers>
<url>New url</url>
<position>New pos</position>
<country>New Country</country>
<category>New category</category>
</item>
<item>
<question>Old question</question>
<answers>Old answers</answers>
<url>Old url</url>
<position>Old pos</position>
<country>Old Country</country>
<category>Old category</category>
</item>
</videos>