When creating an XML file and parsing it, we usually use
Application code example for PHP DOM-XML:
- <? Php
- /**
- * 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 ("< Pre>". Htmlspecialchars ($ doc->Dumpmem ())."</Pre>");
- // 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 ("< Br/> < Br/>");
- // Text node object
- $ Foo = xpath_eval ($ ctx, "// text ");
- Print_r ($ foo );
- Print ("< Br/> < Br/>");
- // The first text node object
- $ Foo = xpath_eval ($ ctx, "// text [1]");
- Print_r ($ foo );
- Print ("< Br/> < Br/>");
- // The second text node object
- $ Foo = xpath_eval ($ ctx, "// text [2]");
- Print_r ($ foo );
- Print ("< Br/> < Br/>");
- // The third answer object
- $ Foo = xpath_eval ($ ctx, "// answer [3]");
- Print_r ($ foo );
- Print ("< Br/> < Br/>");
- // Type, name, and content of the third text node
- $ Foo = xpath_eval ($ ctx, "// text [3]");
- $ Tmp = $ foo->Nodeset;
- Print_r ($ tmp );
- Print ("< Br/>");
- Print ($ tmp [0]->Type ).";";
- Print ($ tmp [0]->Name ).";";
- Print ($ tmp [0]->Content );
- ?>
Note that the PHP DOM-XML can only run on PHPPHP4.0.x + linux