/**
* Domxml function Note
* After connecting Php_domxml.dll
* Get domxml support function with Get_defined_functions ()
*
* Currently Domxml does not support non-iso-8859-1 language declarations
* Support
* Not supported
* So it needs to be transformed into this, which may require
* Utf8_encode () Utf8_decode () function for processing
*
* Function List
* String domxml_version (void) Returns the version number of the Domxml
* Object XmlDoc (String str) DOMDocument object that creates XML from a string
* Object Xmldocfile (string filename) DOMDocument object that creates XML from a file
* Object Xmltree (String str) parses the XML document, returns the tree structure, cannot be changed with the 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 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) creates 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 property
* 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
*
*/
?>
Document XML source tree. XML content
$testxml = '
When reading an XML document, the processor forms a tree, which we call the source tree.
The tree has various types of nodes in the table.
Source tree node
| Node type |
description |
| Root (Root) |
This is the root node of the tree. can appear anywhere in the tree. The root node has only one child node, which refers to the document element node in the XML document. |
| Element (element) |
This node is used for any element in the document. The child nodes of an element node can be element nodes of their content, annotation nodes, processing information nodes, and text nodes. All the text that appears in the |
| text (text) |
document is grouped into a text node. A text node may not have a front or rear sibling node that is immediately followed by a text node. |
| Attribute (properties) |
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. |
| Namespace (name) |
There is a name space node for each element that begins with Xlmns: and the attribute node. These nodes do not have child nodes. |
| processing instruction (processing instructions) |
Each processing instruction has a separate node. None of these nodes have child nodes. |
| Comment (note) |
each has a comment node. None of these nodes have child nodes. |
';
echo "Domxml version:". Domxml_version ();
echo "
";
Xmltree Domxml_dumpmem
$filename = "XML source tree. xml";
$filename = "Resume.xml";
$fp = fopen ($filename, "R");
$inXML = Fread ($fp, FileSize ($filename));
Fclose ($FP);
Delete language Settings
$inXML = Str_replace (' encoding= ' GB2312 "'," ", $inXML);
$inXML = eregi_replace (' encoding= ' [a-z0-9_-]+ ', ', ', $inXML);
$doc = Xmltree ($inXML); Using Xmltree parsing
$myxml = $doc->dumpmem (); Convert to string, header XML version= "1.0"
If executed again, the header will become XML version= "1.0" encoding= "Iso-8859-1"
$myxml = eregi_replace (' &#[0-9]+; ', "", $myxml); Delete
echo "Parsing with Xmltree
";
echo "<textarea cols="60" rows="5">$myxml</textarea>
";
Print_r ($doc); You can see the whole tree also with var_dump ($doc);
XmlDoc
$doc = xmldoc ($inXML);
$myxml = $doc->dumpmem ();
echo "Parsing with xmldoc
";
echo "<textarea cols="60" rows="5">$myxml</textarea>
";
Print_r ($doc); Only the root node can be seen
Domxml_new_xmldoc
$doc = Domxml_new_xmldoc ("1.0");
$root = $doc->add_root ("HTML");
$head = $root->new_child ("Head", "");
$head->new_child ("TITLE", "domxml test 0");
$head->new_child ("TITLE", "Domxml Test 1");
$head->set_attribute ("Language", "GE");
Domxml_node_set_content ($head, "PPP"); Set the contents of the node, multiple executions are superimposed
Domxml_node_set_content ($head, "TTT");
is a function that has only 1-2 "_" in the function name and can be used as an object's method
$myxml = $doc->dumpmem ();
Echo ' Custom XML
";
echo "<textarea cols="60" rows="5">$myxml</textarea>
";
Traversal of a node
/**
Node structure
DomElement Object
Type = 1
tagname = node Name
Domtext Object
Type = 3
Content = Section Contents point
Domcdata Object
Type = 4
Content = Section Contents point
Domprocessinginstruction Object
Type None
target = processing instruction
data = parameter
*/
$ar [] = $doc->root (); Get root node
$ar [] = $ar [count ($ar) -1]->children ();
$ar [] = $ar [count ($ar) -1][0]->children ();
function Domxml_children () cannot return node parameters
The return node parameter requires the use of 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, saved in the array
if (Get_class ($xmldoc) = = "DomDocument") {
$xmlstr = '
'." \ n ";
if (count ($xmldoc->children) = = 1)//root node, no other Members
$docs [] = $xmldoc->root ();
Else
$docs = $xmldoc->children (); Root node, with other members
}else {
$docs = $xmldoc->children (); General node
}
echo __line__. "
";
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. = ">"; End of label
$xmlstr. = Xml_dumpmem ($doc); Enter child node
$xmlstr. = "
Tagname}> "; Closed label
Break
Case 3:
$xmlstr. = $doc->content;
Break
Case 4:
$xmlstr. = "
$xmlstr. = $doc->content;
$xmlstr. = "]]>";
Break
Default
if (Get_class ($doc) = = "Domprocessinginstruction") {
$xmlstr. = "
Target} ";
$xmlstr. = "{$doc->data}?>\n";
}
Break
}
}
return $xmlstr;
}
if (1) {
$filename = "Resume.xml";
$filename = "resume.xsl";
$filename = "XML source tree. xml";
$fp = fopen ($filename, "R");
$inXML = Fread ($fp, FileSize ($filename));
Fclose ($FP);
$inXML = eregi_replace (' encoding= ' [a-z0-9_-]+ ', ', ', $inXML);
$doc = Xmltree ($inXML); Using Xmltree parsing
$doc = xmldoc ($inXML); Using xmldoc parsing
}
cannot be used to parse XSL documents
$myxml = Xml_dumpmem ($doc);
echo "Write a dumpmem on your own without making a mistake.
";
echo "<textarea cols="60" rows="5">$myxml</textarea>
";
Print_r ($doc);
?>
http://www.bkjia.com/PHPjc/313922.html www.bkjia.com true http://www.bkjia.com/PHPjc/313922.html techarticle PHP/** * domxml function Note * After connecting Php_domxml.dll * with Get_defined_functions () to get domxml support function * * Currently domxml does not support language declarations other than iso-8859-1 *? XML vers ...