Domxml Function Notes _php basics

Source: Internet
Author: User
Tags fread processing instruction tagname xsl

<?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>
';

echo "Domxml version:". Domxml_version ();
echo "<p> </p>";
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 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

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"); Sets the contents of a node, and multiple executions are superimposed
Domxml_node_set_content ($head, "TTT");

is a function with only 1-2 "_" in the function name, which can be used as an object method.

$myxml = $doc->dumpmem ();
echo "Custom Xml<br>";
echo "<textarea cols=60 rows=5> $myxml </textarea><br>";

Traversal of nodes
/**
Node structure
DomElement Object
Type = 1
tagname = section Roll Call
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
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;
}

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 an XSL document

$myxml = Xml_dumpmem ($doc);
echo "Write a dumpmem yourself and make no mistake <br>";
echo "<textarea cols=60 rows=5> $myxml </textarea><br>";
Print_r ($doc);

?>
</pre>

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.