How can I add tag <image> into the XML file?

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
nskauto
Forum Newbie
Posts: 4
Joined: Fri May 26, 2006 9:03 am

How can I add tag <image> into the XML file?

Post by nskauto »

Hello guys, please help me.

I have the flash template for Slideshow, but this template uses XML file, where stored paths to images.

XML:

Code: Select all

<gallery timer="1" order="sequential" fadetime="18" looping="yes" xpos="0" ypos="0">
<image path="images/calliandra.jpg" />
<image path="images/dombeya.jpg" />
<image path="images/strelitzia.jpg" />
<image path="images/trensa.jpg" />
<image path="images/treeflower.jpg" />
<image path="images/bellflowers.jpg" />
<image path="images/cordyline.jpg" />
<image path="images/forsythia.jpg" />
</gallery>
I need to understand a simple PHP code to add automatically tag <image path="blabla"> into this XML file .....

Can anybody help me ?

Thank you all.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

See XML with expat

This class is meant to read and write XML documents implementing the DOM API using the PHP XML functions based on the Expat library.

The class is able to open an XML document, parses it, and lets the developer change anything in the document: add nodes, set attributes, remove attributes, clone nodes, insert children, etc..
nskauto
Forum Newbie
Posts: 4
Joined: Fri May 26, 2006 9:03 am

Post by nskauto »

I think there are several lines of code. :(( Please post this simple lines if you know
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Read, edit and write xml file.

Post by xpgeek »

nskauto wrote:I think there are several lines of code. :(( Please post this simple lines if you know
Try it. :D

Code: Select all

$dom = new DomDocument();
$dom->load("article.xml");

$item = $dom->createElement("image");
$item->setAttribute ('path', 'images/someimage.jpg');
$dom->documentElement->appendChild($item);

print $dom->saveXML();
article.xml look like this :)
<gallery timer="1" order="sequential" fadetime="18" looping="yes" xpos="0" ypos="0">
<image path="images/calliandra.jpg" />
<image path="images/dombeya.jpg" />
<image path="images/strelitzia.jpg" />
<image path="images/trensa.jpg" />
<image path="images/treeflower.jpg" />
<image path="images/bellflowers.jpg" />
<image path="images/cordyline.jpg" />
<image path="images/forsythia.jpg" />
</gallery>
nskauto
Forum Newbie
Posts: 4
Joined: Fri May 26, 2006 9:03 am

Post by nskauto »

But I have such errors :(( :

Warning: domdocument() expects at least 1 parameter, 0 given in C:\Inetpub\vhosts\sokieva.bagov.net\httpdocs\test\test.php on line 2
Fatal error: Call to undefined function: load() in C:\Inetpub\vhosts\sokieva.bagov.net\httpdocs\test\test.php on line 3

What happens ? :(
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

my version php is 5.1.2
nskauto
Forum Newbie
Posts: 4
Joined: Fri May 26, 2006 9:03 am

Post by nskauto »

But my PHP Version is 4.3.11.

So in PHP 4 there are others methods and functions :(( Can anybody help me with PHP4?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I believe the standard XML library for PHP 4 is expat: http://us2.php.net/manual/en/ref.xml.php

You can also try using one of the many PEAR pure PHP XML parsing classes (they're kind of slow though): http://pear.php.net/packages.php?catpid=22&catname=XML
Post Reply