0

I have a XML file, videos.xml,

<?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 can I read the file above, and add a new tag <video url="" desc="" /> with the URL of the new video and description of it with PHP, and then overwrite the current videos.xml file so that it gets updated with the new tag.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mana
  • 3
  • 1
  • 2

3 Answers3

4

Using PHP's DOM functions you can load and manipulate it.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
frisco
  • 1,897
  • 2
  • 21
  • 29
  • was wondering, this DOM thing, do i have to install some kind of 3rd party pluging or something to use it? – mana Oct 03 '10 at 19:13
  • frisco: could you show me how to do this by using string functions? – mana Oct 03 '10 at 19:16
  • 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 Oct 03 '10 at 19:16
3

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
  • 4,524
  • 4
  • 42
  • 81
  • 1
    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 Oct 03 '10 at 20:13
  • 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 Oct 03 '10 at 20:30
2

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. ;-)