tags:

views:

103

answers:

3

Hi everyone, i have a videos.xml file,

<?xml version="1.0" encoding="ISO-8859-1" ?>

<videos>
  <video url="videos/Lillebjorn.f4v" desc="Lillebjørn" />
  <video url="videos/Storebjorn.f4v" desc="Storebjørn" />
  <video url="videos/Pegasus.f4v" desc="Pegasus" />
</videos>

I was wondering, how i can read the file above, and add a new tag with the url of the new video and desc of it with php. and then overwrite the current videos.xml file so that it gets updated with the new tag.

+1  A: 

Using PHP DOM functions you can load and manipulate it.

http://php.net/manual/book.dom.php

Also you can modify it by hand using string functions, DOM is probably an overkill here.

EDIT:

Assuming you have the file loaded into the $xml var:

$pos = strpos($xml, "</videos>");

if ($pos === false) {
 $xml = substr($xml,0,$pos)."\t<video url=\"$url\" desc=\"$desc\" />".substr($xml,$pos);
}

To read and write just check http://php.net/manual/en/function.file-get-contents.php http://php.net/manual/en/function.file-put-contents.php

frisco
the file is on the server
mana
was wondering, this DOM thing, do i have to install some kind of 3rd party pluging or something to use it?
mana
frisco: could you show me how to do this by using string functions?
mana
That libary is a PHP extension, it may be enabled or not in your server, if not you have to edit your php.ini to enable it. And it works with XML documents loaded from strings or from local files so there shouldn't be any problems with that.
frisco
Added the a simple code with string functions
frisco
A: 

I strongly disagree with a practice of processing XML as text. Do not learn how to do it wrong, do it right from the start, and the right way to do it is to use DOM processing tools as:

SimpleXMLElement - as the name says - it's simple, or DOMDocument - but for this task SimpleXMLElement would be more than enough.

Start with $videos = simplexml_load_file('videos.xml'); You can modify video object as described in SimpleXMLElement documentation, and then write it back to XML file using file_put_contents('videos.xml', $videos->asXML());

Don't worry, SimpleXMLElement is in each PHP by default.

Harry
Don't take me wrong, using DOM manipulation is the elegant way but it takes a lot more memory and process power to do the same, so you must know what you must use in each case. In the past I have made the same web scrapper using regex and DOM manipulation, yup the second one a lot easier to read and mantain but it was also 10x times slower and even had problems with memory.
frisco
Did you use DOMDocument or SimpleXMLElement? I'll have to check this out, I've always thaught regexes are slower than XML, you've surprised me.
Harry
A: 

SimpleXML isn't included by default when using PHP5 on FreeBSD 8.* (when installed through ports anyway). Issue the commands below as root to get it installed. One sure-fire way to find out if it's installed is to make a script with phpinfo() in it and do a search within your browser for simplexml.

cd /usr/ports/textproc/php5-simplexml 
make install clean

Probably need to restart apache afterwards also.

Hope this saves someone from going bald. ;-)

Michael Hiatt