DOMXML function notes

Source: Internet
Author: User
Tags processing instruction
DOMXML function notes /**
* DOMXML function notes
* Connect to php_domxml.dll
* Use get_defined_functions () to obtain domxml support functions.
*
* Currently, domxml does not support language declarations other than ISO-8859-1.
* Supported
* Not supported
* Therefore, you may need
* Utf8_encode () utf8_decode () function for processing
*
* Function list
* String domxml_version (void) returns the domxml version number.
* Object xmldoc (string str) creates an XML Domdocument object from a string
* Object xmldocfile (string filename) creates an XML Domdocument object from a file
* The object xmltree (string str) parses the xml document and returns the tree structure. it cannot be changed using the domxml function.
* Resource domxml_add_root (resource doc, string name) add the root node
* String domxml_dumpmem (resource doc) converts a domxml object to an XML string. This function has a problem. He will add an extended ascii character before the first Chinese character, such as & # nnn;
* Domxml_node_attributes
* Domxml_elem_get_attribute
* Domxml_elem_set_attribute
* Array domxml_node_children (object doc | node) returns the subnode
* Domxml_node_new_child
* Object domxml_node (string name) creates a node.
* Domxml_node_unlink_node
* Int domxml_node_set_content (resource doc, string content) set node content
* 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 attributes
* Object domxml_get_attribute (resource doc, string name) read attributes
* Domxml_getattr
* Object domxml_set_attribute (resource doc, string name, string value) add attributes
* Domxml_setattr
* Array domxml_children (object doc | node) returns the child node
* Resource domxml_new_child (string name, string content) add a subnode
* Domxml_unlink_node
* Set_content
* New_xmldoc
*
*/
?>

// Document xml source tree. xml content
$ Testxml ='


When reading xml documents, the processor will form a tree called the source tree. The tree has various types of nodes in the table.

Nodes in the source tree









Node type Description
Root) This is the root node of the tree. It can appear anywhere in the tree. The root node has only one subnode. the subnode refers to the document element node in the xml document.
Element) This node is used for any element in the document. A subnode of an element node can be an element node of its content, a comment node, an information processing node, and a text node.
Text) All texts in the document are grouped into text nodes. A text node cannot have a sibling node that is followed by a text node.
Attribute) Each element node has its own set of attribute nodes. The default attribute value is processed in the same way as the specified attribute. None of these nodes have subnodes.
Namespace (name) Each element starting with xlmns: and attribute node has a space node. These nodes do not have subnodes.
Processing Instruction (Processing Command) Each Processing Command has a separate node. None of these nodes have subnodes.
Comment (Comment) Each node has a comment node. None of these nodes have subnodes.


';

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); // use xmltree for parsing
$ Myxml = $ doc-> dumpmem (); // Convert to a string with the 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 "parsed using xmltree
";
Echo"$ Myxml
";
// Print_r ($ doc); // you can see that the entire tree can also be var_dump ($ doc );

// Xmldoc
$ Doc = xmldoc ($ inXML );
$ Myxml = $ doc-> dumpmem ();
Echo "parses with xmldoc
";
Echo"$ Myxml
";
// Print_r ($ doc); // only the root node can be seen

// Domxml_new_xmldoc
$ Doc = domxml_new_xmldocdoc ("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 content of the node. multiple executions are superimposed.
Domxml_node_set_content ($ head, "ttt ");

// Only one or two "_" functions in the function name can be used as object methods.

$ Myxml = $ doc-> dumpmem ();
Echo "custom xml
";
Echo"$ Myxml
";

// Node traversal
/**
Node structure
DomElement Object
Type = 1
Tagname = node name
DomText Object
Type = 3
Content = content
DomCData Object
Type = 4
Content = content

DomProcessingInstruction Object
Type None
Target = Processing Command
Data = parameter

*/
$ Ar [] = $ doc-> root (); // get the root node
$ Ar [] = $ ar [count ($ ar)-1]-> children ();
$ Ar [] = $ ar [count ($ ar)-1] [0]-> children ();

// The function domxml_children () cannot return node parameters
// Domxml_attributes () is required for the return node parameter ()
// Var_dump (domxml_attributes ($ head ));
// Print_r ($ ar [1] [0]-> attributes ());
// Print_r ($ ar );

Function xml_dumpmem ($ xmldoc ){
Static $ mode = 0;
$ Xmlstr = "";
// Get the node and save it 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 (); // common node
}

// Echo _ LINE __."
";
Foreach ($ docs as $ doc ){
$ Attr = $ doc-> attributes ();
Switch ($ doc-> type ){
Case 1:
$ Xmlstr. = "<{$ doc-> tagname}"; // tag header
If ($ attr ){
Foreach ($ attr as $ key)
$ Xmlstr. = "{$ key-> name }=\" {$ key-> value} \ ""; // tag parameters
}
$ Xmlstr. = ">"; // tag end
$ Xmlstr. = xml_dumpmem ($ doc); // enter the subnode
$ Xmlstr. =" Tagname}> "; // Close the tag
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); // use xmltree for parsing
$ Doc = xmldoc ($ inXML); // use xmldoc for parsing
}

// Cannot be used to parse the xsl document

$ Myxml = xml_dumpmem ($ doc );
Echo "write a dumpmem by yourself.
";
Echo"$ Myxml
";
Print_r ($ doc );

?>

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.