Forum Moderators: open

Message Too Old, No Replies

Adding child/nodes to existing XML using PHP DOM

dev. server running PHP 5.04

         

sneaks

3:33 am on Aug 31, 2005 (gmt 0)

10+ Year Member



I posted this in the PHP discussion but wanted to raise this in the XML group too... Playing around with php/xml and i am having issue inserting a simple news item with 2 child nodes to an existing xml file which follows the structure below. It appends to the end of the file and I would like to have the news <item> and its child data append within the <news> tag .. any help/advice/references would be greatly appreciated.

Thanks!
j. :)
------------------------------------

<xml version="1.0">
<news>
<item>
<date>20050830</date>
<title>Title</title>
</item>
<item>
<date>20050829</date>
<title>Another TItle</title>
</item>
</news>

------------------------------------

<?php
$doc = new DOMDocument;
$doc->load('news/newsIndex.xml');
$doc->formatOutput = true;

$NewsItem = $doc->createElement('Item');
$NewsItem = $doc->appendChild($NewsItem);

$NewsDate = $doc->createElement('Date');
$NewsDate = $NewsItem->appendChild($NewsDate);

$DateValue = $doc->createTextNode('19720502');
$DateValue = $NewsDate->appendChild($DateValue);

$NewsTitle = $doc->createElement('Title');
$NewsTitle = $NewsItem->appendChild($NewsTitle);

$TitleValue = $doc->createTextNode('Test Title');
$TitleValue = $NewsTitle->appendChild($TitleValue);

$test = $doc->save("news/newstest.xml");
print 'done.';
?>