Use PHP DOM-XML to create and parse XML files

Source: Internet
Author: User

Use PHP DOM-XML to create and parse XML files

<? 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/> ");
// Text Node object
$ Foo = xpath_eval ($ CTX, "// text ");
Print_r ($ Foo );
Print ("<br/> ");
// The first text Node object
$ Foo = xpath_eval ($ CTX, "// text [1]");
Print_r ($ Foo );
Print ("<br/> ");
// The second text Node object
$ Foo = xpath_eval ($ CTX, "// text [2]");
Print_r ($ Foo );
Print ("<br/> ");
// The third answer object
$ Foo = xpath_eval ($ CTX, "// Answer [3]");
Print_r ($ Foo );
Print ("<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 );

?>

It should be noted that PHP Dom can only run on PHP php4.0.x + linux

for the phpdom class library, go to http://www.zend.com/downloaddownload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.