? Php *** Topic: CreateandparseXMLfilesusingPHPDOM-XML * Source: www. php. netdomxml * Reference: www.zugeschaut-und-mitgebaut.dephpextension.domxml.html * Author: urs@circle.ch, 16-1-2001 ** use PHPDOM-XML to create and parse XML
/**
* Topic: Create and parse XML files using PHP DOM-XML
* Source: http://www.php.net/domxml
* Reference: http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html
* Author: urs@circle.ch, 16-1-2001
*
*/
// Use the PHP DOM-XML to create and parse XML files
// Create an XML document object. Subsequent processing will be performed on this basis.
$ Doc = new_xmldoc ("1.0 ");
// Create a root node and set an attribute
$ Root = $ doc-> add_root ("faq ");
$ Root-> setattr ("page", "32 ");
// Subnode
$ One = $ root-> new_child ("question ","");
// Set attributes for the subnode
$ One-> setattr ("number", "1 ");
// Question also creates a subnode and assigns a value to it
$ One-> new_child ("text", "1. Where to get libxml-2.0.0? ");
$ One-> new_child ("answer", "You can download the latest
Release of libxml either as a source archive or
RPM package from http://www.xmlsoft.org.
The current version is libxml2-2.2.1 .");
$ Two = $ root-> new_child ("question ","");
$ Two-> setattr ("number", "2 ");
$ Two-> new_child ("text", "2. How to configure PHP4? ");
// Create a node with no direct value assignment
$ Twoone = $ two-> new_child ("answer ","");
// Assign a value to it separately
$ Twoone-> set_content ("DIR is the libxml install directory
(If you just use -- with-dom it ULTS
To/usr), I needed to use -- with-dom =/usr/local ");
$ Three = $ root-> new_child ("question ","");
$ Three-> setattr ("number", "7 ");
$ Three-> new_child ("text", "7. How to use dom xml function? ");
$ Three-> new_child ("answer", "Read this document source
A simple example .");
// Output to Browser
Print ("
".htmlspecialchars($doc->dumpmem() )."
");
// Write to file
// Write back to file
$ Fp = fopen ("test_dom.xml", "w + ");
Fwrite ($ fp, $ doc-> dumpmem (), strlen ($ doc-> dumpmem ()));
Fclose ($ fp );
//------------------------------------------------------
// Now use xpath to get content from the XML document
$ Doc = xmldoc (join ("", file ("test_dom.xml ")));
$ Ctx = xpath_new_context ($ doc );
// All objects
$ Foo = xpath_eval ($ ctx, "// child ::*");
Print_r ($ foo );
Print ("
");
// Text node object
$ Foo = xpath_eval ($ ctx, "// text ");
Print_r ($ foo );
Print ("
");
// The first text node object
$ Foo = xpath_eval ($ ctx, "// text [1]");
Print_r ($ foo );
Print ("
");
// The second text node object
$ Foo = xpath_eval ($ ctx, "// text [2]");
Print_r ($ foo );
Print ("
");
// The third answer object
$ Foo = xpath_eval ($ ctx, "// answer [3]");
Print_r ($ foo );
Print ("
");
// Type, name, and content of the third text node
$ Foo = xpath_eval ($ ctx, "// text [3]");
$ Tmp = $ foo-> nodeset;
Print_r ($ tmp );
Print ("
");
Print ($ tmp [0]-> type ).";";
Print ($ tmp [0]-> name ).";";
Print ($ tmp [0]-> content );
?>
It should be noted that php dom can only run on PHP PHP4.0.x + linux
Php dom class library go to http://www.zend.com/download