search
HomeBackend DevelopmentPHP TutorialDOMXML function notes_PHP tutorial

DOMXML function notes_PHP tutorial

Jul 21, 2016 pm 04:11 PM
definedgetphpfunctionbackusenotesconnect


/**
* 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<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 .= "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 .= "                    $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>echo "<textarea cols="60" rows="5">$myxml</textarea><br>";<br>print_r($doc);<br><br>?><br></textarea>





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...
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment