Create and resolve XML files using PHP DOM-XML

zhaozj2021-02-17  49

// Create an XML document object; after the process of processing will perform $ doc = new_xmldoc ("1.0") on this basis;

// Create a root node and set an attribute $ root = $ doc-> add_root ("FAQ"); $ root-> setattr ("Page", "32");

// Sub-node $ ONE = $ root-> new_child ("Question", "); // Set the property $ ONE-> Setattr (" Number "," 1 "); // Question also creates a child node And give 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 that is not directly assigned $ Twoone = $ Two-> New_Child (" Answer ","); // then assigns it to $ Twoone-> set_content ("Dir is the libxml install directory) If you just use --with-dom it defaults 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 for 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 ("," 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 (""); // First TEXT Node object $ foo = xpath_eval ($ CTX, "/ / text [1] "); Print_R ($ foo); Print (">"); // 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 ("");

// The third TEXT Node type, name and content $ foo = xpath_eval ($ CTX, "// text [3]"); $ TMP = $ foo-> node; print_r ($ tmp); print ("< Br /> "); Print ($ TMP [0] -> TYPE)."; "; Print ($ TMP [0] -> Name)."; "; Print ($ TMP [0] -> content);

?>

It should be explained that PHP DOM can only run on PHP php4.0.x linux

PHPDOM class library please go to www.zend.com/download.com

转载请注明原文地址:https://www.9cbs.com/read-30449.html

New Post(0)