PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
samir_gambler
Forum Newbie
Posts: 19 Joined: Wed Mar 31, 2010 9:03 am
Post
by samir_gambler » Sat May 08, 2010 10:23 pm
I need to append a new node to existing XML file with the help of PHP 5. I am new to DOM. Can anyone plz guide me.....
MY xml file is play.xml
Code: Select all
<rss version="2.0">
<channel>
<title>Title of page</title>
<link>http://www.foo.com</link>
<description>Description of page</description>
<item>
<title>Story about something</title>
<link>#</link>
<enclosure url="demo.flv"
type="video/quicktime"/>
</item>
<item>
<title>The Wolfman</title>
<link>#</link>
<enclosure url="wolfman.flv"
type="video/quicktime"/>
</item>
</channel>
</rss>
Zyxist
Forum Contributor
Posts: 104 Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland
Post
by Zyxist » Sun May 09, 2010 2:02 am
There are a lot of tutorials that explain how to use DOM:
http://devzone.zend.com/article/2387-XML-and-PHP-5
http://www.w3schools.com/php/php_xml_dom.asp
They should help you start. Remember that DOM is a language-independent API, which means that you could take a tutorial for a different language and simply do exactly the same in PHP. On W3Schools, there is a tutorial for DOM, where you find concrete information how to append a node.
samir_gambler
Forum Newbie
Posts: 19 Joined: Wed Mar 31, 2010 9:03 am
Post
by samir_gambler » Sun May 09, 2010 11:47 am
i am able to append new node to XML file but have some issue in RSS + Media XML file
My RSS+Media xml file is
Code: Select all
<rss version="2.0">
<channel>
<title>Title of page</title>
<link>http://www.foo.com</link>
<description>Description of page</description>
<item>
<title>Story about something</title>
<link>#</link>
<enclosure url="demo.flv"
type="video/quicktime"/>
</item>
<item>
<title>The Wolfman</title>
<link>#</link>
<enclosure url="wolfman.flv"
type="video/quicktime"/>
</item>
</channel>
</rss>
My php code for appending new entry is
Code: Select all
$dom = new DomDocument();
$dom->load("articles.xml");
$item = $dom->createElement("item");
$title = $dom->createElement("title");
$titletext = $dom->createTextNode("XML in PHP5");
$title->appendChild($titletext);
$item->appendChild($title);
$title = $dom->createElement("link");
$titletext = $dom->createTextNode("samir");
$title->appendChild($titletext);
$item->appendChild($title);
//$item = $dom->createElement("enclosure url=\"wolfman.flv\" type=\"video/quicktime/");
$dom->documentElement->appendChild($item);
//print $dom->saveXML();
$dom->save("articles.xml")
i am unable to add <enclosure url=.....\> information plz help its really urgent...
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Sun May 09, 2010 12:36 pm