<?php
/**
* Domxml Function notes
* After connecting Php_domxml.dll
* Get domxml support function with Get_defined_functions ()
*
* Currently, Domxml does not support language declarations other than iso-8859-1
* <?xml version= "1.0" encoding= "iso-8859-1"?> support
* <?xml version= "1.0" encoding= "gb2312"?> not supported
* <?xml version= "1.0"?> therefore need to be transformed into this, may require
* Utf8_encode () Utf8_decode () function for processing
*
* List of functions
* String domxml_version (void) Returns the version number of Domxml
* Object XmlDoc (String str) creates DOMDocument objects of XML from strings
* Object Xmldocfile (string filename) creates XML DOMDocument objects from a file
* Object Xmltree (String str) parses an XML document, returns a tree structure and cannot be changed with a domxml function
* Resource Domxml_add_root (Resource doc,string name) add root node
* String Domxml_dumpmem (Resource doc) converts the Domxml object into an XML string. This function has a problem, he will precede the first Chinese character with an extended ASCII character, such as & #nnn;
* Domxml_node_attributes
* Domxml_elem_get_attribute
* Domxml_elem_set_attribute
* Array Domxml_node_children (object doc|node) returns child nodes
* Domxml_node_new_child
* Object Domxml_node (string name) to create a node node
* Domxml_node_unlink_node
* int Domxml_node_set_content (resource doc,string content) Set node contents
* Object Domxml_new_xmldoc (string version) creates a new empty XML object
* Xpath_new_context
* Xpath_eval
* Xpath_eval_expression
* Xptr_new_context
* Xptr_eval
* Object Domxml_root (object doc) returns the root node
* Array Domxml_attributes (resource note) Get node properties
* Object Domxml_get_attribute (Resource doc,string name) Read properties
* Domxml_getattr
* Object Domxml_set_attribute (Resource doc,string name,string value) add attribute
* Domxml_setattr
* Array Domxml_children (object doc|node) returns child nodes
* Resource Domxml_new_child (string name,string content) Add child nodes
* Domxml_unlink_node
* Set_content
* New_xmldoc
*
*/
?>
<pre>
<?php
Document XML source tree. XML content
$testxml = '
<?xml version= "1.0" encoding= "GB2312"?>
<root>
<note> when reading an XML document, the processor will form a tree, which we call the source tree. The tree has various types of nodes in the table.
</note>
<title> the node that the source tree has </title>
<table>
<tr><th> node type </th><th> description </th></tr>
<tr><td>root (Root) </td><td> This is the root node of the tree. can appear anywhere in the tree. The root node has only one child node, and the child node refers to the document element node in the XML document. </td></tr>
<tr><td>element (Element) </td><td> This node is used for any element in the document. The child nodes of an element node can be element nodes of its content, annotation nodes, processing information nodes, and text nodes. </td></tr>
<tr><td>text (text) </td><td> all text that appears in a document is grouped into text nodes. Text nodes may not have the immediately preceding or following sibling nodes of the same text node. </td></tr>
<tr><td>attribute (properties) </td><td> each element node has its own attached attribute node. The default property value is handled in the same way as the specified property. None of these nodes have child nodes. </td></tr>
<tr><td>namespace (name) </td><td> has a name space node for each element that starts with a xlmns: and an attribute node. These nodes do not have child nodes. </td></tr>
<tr><td>processing instruction (processing instruction) </td><td> each processing instruction has a separate node. None of these nodes have child nodes. </td></tr>
<tr><td>comment (note) </td><td> each has an annotation node. None of these nodes have child nodes. </td></tr>
</table>
</root>
';
$doc = Xmltree ($inXML); Using Xmltree parsing
$myxml = $doc->dumpmem (); Convert to string, header to XML version= "1.0"
If you do this again, the header will become an XML version= "1.0" encoding= "Iso-8859-1"
$myxml = eregi_replace (' &#[0-9]+; ', ' "", $myxml); Delete
echo "Analytical <br> with Xmltree";
echo "<textarea cols=60 rows=5> $myxml </textarea><br>";
Print_r ($doc); You can see the whole tree may also use Var_dump ($doc);
XmlDoc
$doc = xmldoc ($inXML);
$myxml = $doc->dumpmem ();
echo "Analytical <br> with xmldoc";
echo "<textarea cols=60 rows=5> $myxml </textarea><br>";
Print_r ($doc); Only the root node can be seen
function Domxml_children () cannot return node parameters
Return node parameters need to use Domxml_attributes ()
Var_dump (Domxml_attributes ($head));
Print_r ($ar [1][0]->attributes ());
Print_r ($ar);
function Xml_dumpmem ($xmldoc) {
static $mode = 0;
$xmlstr = "";
Gets the node, which is saved in the array
if (Get_class ($xmldoc) = = "DOMDocument") {
$xmlstr = ' <?xml version= ' 1.0 ' encoding= ' gb2312 '?> '. \ n ";
if (count ($xmldoc->children) = = 1)//root node, no other member
$docs [] = $xmldoc->root ();
Else
$docs = $xmldoc->children (); Root node, with other members
}else {
$docs = $xmldoc->children (); General node
}
echo __line__. " <br> ";
foreach ($docs as $doc) {
$attr = $doc->attributes ();
Switch ($doc->type) {
Case 1:
$xmlstr. = "<{$doc->tagname}"; Label Header
if ($attr) {
foreach ($attr as $key)
$xmlstr. = "{$key->name}=\" {$key->value}\ ""; Label parameters
}
$xmlstr. = ">"; Tag End
$xmlstr. = Xml_dumpmem ($doc); Enter child node
$xmlstr. = "</{$doc->tagname}>"; Closed label
Break
Case 3:
$xmlstr. = $doc->content;
Break
Case 4:
$xmlstr. = "<! [CDATA] [";
$xmlstr. = $doc->content;
$xmlstr. = "]]>";
Break
Default
if (Get_class ($doc) = = "Domprocessinginstruction") {
$xmlstr. = " {$doc->target} ";
$xmlstr. = "{$doc->data}?>\n";
}
Break
}
}
return $xmlstr;
}
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.