<?php
// Author: Mike Neugebauer
// Class which builds XML nodes include_once( "XmlNode.php" );
// This is a fairly crude example which demonstrates how XmlNode can // be used to build and display XHTML data. // It might take a little getting used to, but it's better that trying // to remember to put all the closing tags in the right place. // Begin building an XHTML document.
$doc = new XmlNode( "html", EMPTY_CDATA, NO_ATTRS, null, false ); $head = new XmlNode( "head", EMPTY_CDATA, NO_ATTRS, $doc ); $curr = new XmlNode( "title", "Welcome to the XmlNode Demo", NO_ATTRS, $head ); $body = new XmlNode( "body", EMPTY_CDATA, array( "bgcolor"=>"#cccccc" ), $doc ); $div = new XmlNode( "div", EMPTY_CDATA, array( "align"=>"center" ), $body ); $curr = new CdataNode( "A bunch", $div ); $img = new XmlNode( "hr", EMPTY_CDATA, array( "noshade"=>"noshade" ), $div ); $para = new XmlNode( "p", EMPTY_CDATA, array( "align"=>"left" ), $div, true ); $curr = new CdataNode( "of ", $para ); $curr = new XmlNode( "b", "stuff ", NO_ATTRS, $para ); $curr = new XmlNode( "i", "thrown into ", NO_ATTRS, $div ); $curr = new CdataNode( "a Div.", $div ); // You can also add child/children using the addChild() and addChildren() methods $simple_br = new XmlNode( "br", EMPTY_CDATA ); $body->addChild( $simple_br ); $body->addChildren ( array( $simple_br, $simple_br ) ); // Each child is sent a reference to its parent. $table = new XmlNode( "table", EMPTY_CDATA, array( "border"=>"2", "align"=>"center", "width"=>480 ), $body ); $curr_row = new XmlNode( "tr", EMPTY_CDATA, array( "align"=>"left" ), $table ); $curr_cell = new XmlNode( "td", "piece 1", array( "bgcolor"=>"#666666" ), $curr_row ); $curr_cell = new XmlNode( "td", "piece 2", array( "bgcolor"=>"#00ff00" ), $curr_row ); $curr_row = new XmlNode( "tr", EMPTY_CDATA, array( "align"=>"right" ), $table ); $curr_cell = new XmlNode( "td", "piece 3", array( "bgcolor"=>"#ff0000" ), $curr_row ); $curr_cell = new XmlNode( "td", "piece 4", array( "bgcolor"=>"#0000ff" ), $curr_row ); // write XHTML document echo ( $doc->getXhtml() );
// Uncomment the line below to write the XHTML document to a file // $doc->writeToFile( "/tmp/testnode.html" ); // Convert XHTML to SimpleXML, run an XPath query $xml_obj = $doc->toSimpleXml(); $get = $xml_obj->xpath( "/html/head/title" ); echo "Grab Title: " . $get[0]; ?>
|