XML and PHP 5

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

Post Reply
samir_gambler
Forum Newbie
Posts: 19
Joined: Wed Mar 31, 2010 9:03 am

XML and PHP 5

Post by samir_gambler »

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>
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: XML and PHP 5

Post by Zyxist »

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

Re: XML and PHP 5

Post by samir_gambler »

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...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: XML and PHP 5

Post by Weirdan »

Post Reply