O'Reilly logo

Learning PHP 5 by David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Advanced XML Processing

SimpleXML is just the tip of PHP 5's new XML processing capabilities. The DOM functions give you exacting control over all aspects of an XML document, and you can also do XSL transformations, XPath queries, and XInclude processing, as well as execute an extravagant, exhaustive exaltation of other exciting and exotic XML exercises.

Example 13-10 shows an RSS feed-handling class based on the built-in DomDocument class. The addItem( ) method of the RSS class is used to add a new item to the feed.

Example 13-10. Extending DomDocument to handle an RSS feed

class RSS extends DomDocument { function _ _construct($title, $link, $description) { // Set this document up as XML 1.0 with a root // <rss> element that has a version="0.91" attribute parent::_ _construct('1.0'); $rss = $this->createElement('rss'); $rss->setAttribute('version', '0.91'); $this->appendChild($rss); // Create a <channel> element with <title>, <link>, // and <description> sub-elements $channel = $this->createElement('channel'); $channel->appendChild($this->makeTextNode('title', $title)); $channel->appendChild($this->makeTextNode('link', $link)); $channel->appendChild($this->makeTextNode('description', $description)); // Add <channel> underneath <rss> $rss->appendChild($channel); // Set up output to print with linebreaks and spacing $this->formatOutput = true; } // This function adds an <item> to the <channel> function addItem($title, $link, $description) { // Create an <item> element with <title>, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required