Creating and parsing XML files using PHP dom-xml

Source: Internet
Author: User
dom|xml| Create <?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
*
*/
Creating and parsing XML files using PHP dom-xml
Creates an XML Document object, which is based on the following process
$doc = New_xmldoc ("1.0");

Create the root node and set a property
$root = $doc->add_root ("FAQ");
$root->setattr ("page", "32");

Child nodes
$one = $root->new_child ("question", "");
Set properties for child nodes
$one->setattr ("number", "1");
Question also creates a child node and assigns it a value
$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 does not directly assign a value
$twoone = $two->new_child ("Answer", "");
and give it a separate assignment.
$twoone->set_content ("DIR is" libxml install directory
(If you are 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 do I use DOM XML function? ");
$three->new_child ("Answer", "Read this document source for
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 an 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/>");
First text Node object
$foo = Xpath_eval ($ctx, "//text[1]");
Print_r ($foo);
Print ("<br/><br/>");
Second Text Node object
$foo = Xpath_eval ($ctx, "//text[2]");
Print_r ($foo);
Print ("<br/><br/>");
Third Answer Object
$foo = Xpath_eval ($ctx, "//answer[3]");
Print_r ($foo);
Print ("<br/><br/>");

The 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 needs to be explained that the PHP DOM can only run on PHP php4.0.x + Linux

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.