Home > Backend Development > PHP Tutorial > DOMXML function notes_PHP tutorial

DOMXML function notes_PHP tutorial

WBOY
Release: 2016-07-21 16:11:32
Original
966 people have browsed it


/**
* DOMXML function notes
* After connecting php_domxml.dll
* Use get_defined_functions() to get domxml support functions
*
* Currently domxml does not support language declarations other than iso-8859-1
* Supported
* Not supported
* Therefore, it needs to be transformed into this, which may require
* utf8_encode() utf8_decode() functions for processing
*
* function list
* string domxml_version(void) Returns the version number of domxml
* object xmldoc(string str) Creates an XML Domdocument object from a string
* object xmldocfile(string filename) Creates an XML Domdocument object from a file
* object xmltree(string str) Parses the xml document and returns the tree structure, which cannot be changed with the domxml function XML string. There is a problem with this function. It will add an extended ascii character in front of the first Chinese character, in the form of nnn;
* domxml_node_attributes
* domxml_elem_get_attribute
* domxml_elem_set_attribute
* array domxml_node_children(object doc |node) Return child node
* domxml_node_new_child
* object domxml_node(string name) Create a node node
* domxml_node_unlink_node
* int domxml_node_set_content(resource doc,string content) Set node content
* object domxml_new_xmldoc(string version) Create a new empty XML object
* xpath_new_context
* xpath_eval
* xpath_eval_expression
* xptr_new_context
* xptr_eval
* object domxml_root(object doc) Return the root node
* array domxml_attributes(resource note) Get node attributes
* object domxml_get_attribute(resource doc,string name) Read attributes
* domxml_getattr
* object domxml_set_attribute(resource doc,string name,string value) Add attribute
* domxml_setattr
* array domxml_children(object doc|node) Return child node
* resource domxml_new_child(string name,string content) Add child node
* domxml_unlink_node
* set_content
* new_xmldoc
*
*/
?>

<br><?php<br>// Document xml source tree.xml content<br> $testxml = '<br><?xml version="1.0" encoding="GB2312"?> <br><root><br><note>When reading the xml document, the processor will form a tree , we call it the source tree. The tree has various types of nodes in the table. <br></note><br><title>The source tree has node</title><br><table><br><tr><th>Node type</th> <th>Description</th></tr><br><tr><td>Root (root)</td><td>This is the root node of the tree. Can appear anywhere on 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><br><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, annotation nodes, processing information nodes, and text nodes of its content. </td></tr><br><tr><td>Text(text)</td><td>All text appearing in the document are grouped into text nodes. A text node cannot have any immediately preceding or following sibling nodes that are also text nodes. </td></tr><br><tr><td>Attribute (attribute)</td><td>Each element node has its own set of attached attribute nodes. Default property values ​​are handled in the same way as specified properties. None of these nodes have child nodes. </td></tr><br><tr><td>Namespace(name)</td><td>For every element starting with xlmns: and an attribute node, there is a Name space node. These nodes have no child nodes. </td></tr><br><tr><td>Processing Instruction(processing instruction)</td><td>Each processing instruction has a separate node. None of these nodes have child nodes. </td></tr><br><tr><td>Comment (Comment)</td><td>Each has a comment node. None of these nodes have child nodes.</td></tr><br></table><br></root><br>';<br><br>echo "domxml version:".domxml_version();<br>echo "<p> </p>";<br>// xmltree domxml_dumpmem<br>$filename = "xml source tree.xml";<br>//$filename = "resume.xml";<br>$fp = fopen($filename,"r"); <br>$inXML = fread($fp,filesize($filename)); <br>fclose($fp); <br>// Delete language setting Define<br>//$inXML = str_replace(' encoding="GB2312"',"",$inXML);<br>$inXML = eregi_replace(' encoding="[a-z0-9_-]+"', "",$inXML);<br><br>$doc = xmltree($inXML); // Use xmltree to parse <br>$myxml = $doc->dumpmem(); // Convert to string, header For xml version = "1.0" <br> // If you execute it again, your head will become xml version = "1.0" encoding = "ISO-8859-1" <br> // $ Myxml = EREGI_REPLE ('[[[' [[ 0-9]+;',"",$myxml); // Delete<br>echo "Use xmltree to parse<br>";<br>echo "<textarea cols=60 rows=5>$myxml< /textarea><br>";<br>//print_r($doc); // You can see the entire tree or use var_dump($doc);<br><br>// xmldoc<br>$doc = xmldoc($inXML); <br>$myxml = $doc->dumpmem();<br>echo "Use xmldoc to parse<br>";<br>echo "<textarea cols=60 rows=5> $myxml</textarea><br>";<br>//print_r($doc); // Only the root node can be seen<br><br>// domxml_new_xmldoc<br>$doc = domxml_new_xmldoc("1.0 ");<br><br>$root = $doc->add_root("HTML");<br>$head = $root->new_child("HEAD", "");<br>$head ->new_child("TITLE", "DOMXML Test 0");<br>$head->new_child("TITLE", "DOMXML Test 1");<br>$head->set_attribute("Language" , "ge");<br>domxml_node_set_content($head,"ppp"); // Set the content of the node, multiple executions are superimposed<br>domxml_node_set_content($head,"ttt");<br><br>// It is a function with only 1-2 "_" in the function name, which can be used as an object method <br><br>$myxml = $doc->dumpmem();<br>echo "custom xml<br>";<br>echo "<textarea cols=60 rows=5>$myxml</textarea><br>";<br><br>// Traversal of nodes<br>/** <br> Node Structure<br> DomElement Object<br> type = 1<br> tagname = Node Name<br> DomText Object<br> type = 3<br> content = Section Content Point<br> DomCData Object<br> type = 4<br> content = section content point<br><br> DomProcessingInstruction Object<br> type none<br> target = processing instruction<br> data = parameter<br><br>*/<br>$ar[] = $doc->root(); // Get the root node <br>$ar[] = $ar[count($ar)-1]->children ();<br>$ar[] = $ar[count($ar)-1][0]->children();<br><br>// Function domxml_children() cannot return node parameters<br>//To return node parameters, you need to use domxml_attributes()<br>//var_dump(domxml_attributes($head));<br>//print_r($ar[1][0]->attributes());<br>//print_r($ar);<br><br>function xml_dumpmem($xmldoc) {<br> static $mode = 0;<br> $xmlstr = "";<br> // Get the node and save it in In the array<br> if(get_class($xmldoc) == "DomDocument") {<br> $xmlstr = '<?xml version="1.0" encoding="gb2312"?>'."n";<br> if(count($xmldoc->children) == 1) // Root node, no other members <br> $docs[] = $xmldoc->root();<br> else<br> $ docs = $xmldoc->children(); // Root node, with other members <br> }else {<br> $docs = $xmldoc->children(); // General node <br> }<br><br>// echo __LINE__."<br>";<br> foreach($docs as $doc) {<br> $attr = $doc->attributes();<br> switch($doc ->type) {<br> case 1:<br> $xmlstr .= "<{$doc->tagname}"; // Tag header<br> if($attr) {<br> foreach( $attr as $key)<br>                                                                                                 $xmlstr<br> $xmlstr .= ">"; // End of tag <br> $xmlstr .= xml_dumpmem($doc); // Enter child node <br> $xmlstr .= "</{$doc-&gt ;tagname}>"; // Close tag<br> break;<br> case 3:<br> $xmlstr .= $doc->content;<br> case 4:<br> $xmlstr .= "<![CDATA][";<br>                    $xmlstr                                                                   default:<br> if(get_class($doc) == "DomProcessingInstruction") {<br> $xmlstr .= "<?{$doc->target}";<br> $xmlstr .= " {$ doc->data}?>n";<br> }<br> break;<br> }<br> }<br> return $xmlstr;<br>}<br><br>if(1) {<br> $filename = "resume.xml";<br>// $filename = "resume.xsl";<br> $filename = "xml source tree.xml";<br> $fp = fopen($ filename,"r"); <br> $inXML = fread($fp,filesize($filename)); <br> fclose($fp); <br> $inXML = eregi_replace(' encoding="[a-z0 -9_-]+"',"",$inXML);<br>// $doc = xmltree($inXML); // Use xmltree to parse <br> $doc = xmldoc($inXML); // Use xmldoc Parse<br>}<br><br>// Cannot be used to parse xsl documents<br><br>$myxml = xml_dumpmem($doc);<br>echo "Write your own dumpmem and you won’t go wrong<br&gt ;";<br>echo "<textarea cols=60 rows=5>$myxml</textarea><br>";<br>print_r($doc);<br><br>?><br>





http://www.bkjia.com/PHPjc/313922.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/313922.htmlTechArticle?php /** * DOMXML function notes* After connecting php_domxml.dll* Use get_defined_functions() to get the domxml support function * * Currently domxml does not support language declarations other than iso-8859-1* ?xml vers...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template