Unable to insertBefore on an XML file

miguelhornet

New Member
I'm trying to write a script that will update an RSS XML file. I want it to take the existing file and add a new item to the top of the items list. I've previous gotten it to add to the end of the file, but now it's currently not adding the new item at all. I've been checking around online, but I still can't get it to work. Here is what I have so far:\[code\]$rssDoc = new DOMDocument();$rss_file = $_SERVER['DOCUMENT_ROOT'].'/test_site/feed.xml';$rssDoc->load($rss_file);$items = $rssDoc->getElementsByTagName('item');$newItem = $rssDoc->createElement('item');$rssTitle = $rssDoc->createElement('title');$rssTitle->appendChild($rssDoc->createTextNode($title));$newItem->appendChild($rssTitle);$rssDesc = $rssDoc->createElement('description');$rssDesc->appendChild($rssDoc->createTextNode($string));$newItem->appendChild($rssDesc);$rssLink = $rssDoc->createElement('link');$rssLink->appendChild($rssDoc->createTextNode($link));$newItem->appendChild($rssLink);$rssDate = $rssDoc->createElement('pubDate');$rssDate->appendChild($rssDoc->createTextNode($pubDate));$newItem->appendChild($rssDate);$firstItem = $items->item(0);$firstItem->insertBefore($newItem,$firstItem->firstChild);$rssDoc->formatOutput = true;echo $rssDoc->saveXML();\[/code\]What am I missing?
 
Back
Top