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
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.