Tutorials to .com

Tutorials to .com » Php » Xml » Notes DOMXML function

Notes DOMXML function

Print View , by: iSee ,Total views: 10 ,Word Count: 1753 ,Date: Thu, 16 Apr 2009 Time: 12:51 AM

<? php
/ **
Notes * DOMXML function
* After the connection php_domxml.dll
* Use get_defined_functions () function has been to support domxml
*
* Domxml currently do not support the non-iso-8859-1 other than the language of the statement
* <? xml version = "1.0" encoding = "iso-8859-1"?> Support
* <? xml version = "1.0" encoding = "gb2312"?> Do not support
* <? xml version = "1.0"?> Therefore need to be transformed into this, it may be necessary
* Utf8_encode () utf8_decode () function to deal with
*
* Function list
* String domxml_version (void) return the version number domxml
* Object xmldoc (string str) string to create XML from the object of Domdocument
* Object xmldocfile (string filename) to create XML from the document object of Domdocument
* Object xmltree (string str) Analysis of xml documents, return to the tree structure, and function can not be used to change the domxml
* Resource domxml_add_root (resource doc, string name) Add a root node
* String domxml_dumpmem (resource doc) will domxml object into XML string. This function have a question, he will be the first Canadian of Chinese characters in front of an extended ascii characters, like the & # nnn;
* Domxml_node_attributes
* Domxml_elem_get_attribute
* Domxml_elem_set_attribute
* Array domxml_node_children (object doc | node) return sub-node
* 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 the node content
* Object domxml_new_xmldoc (string version) to create a new empty XML Object
* Xpath_new_context
* Xpath_eval
* Xpath_eval_expression
* Xptr_new_context
* Xptr_eval
* Object domxml_root (object doc) to return to root
* Array domxml_attributes (resource note) access node attributes
* Object domxml_get_attribute (resource doc, string name) to 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 sub-node
* Resource domxml_new_child (string name, string content) to add child nodes
* Domxml_unlink_node
* Set_content
* New_xmldoc
*
* /
?>
<pre>
<? php
/ / Xml source document tree. Xml content
$ testxml = '
<? xml version = "1.0" encoding = "GB2312"?>
<root>
<note> read xml document, the processor will be a tree, we will be known as the source of their tree. The tree with a table of various types of nodes.
</ note>
<title> source tree with the node </ title>
<table>
<tr> <th> node type </ th> <th> Description </ th> </ tr>
<tr> <td> Root (root) </ td> <td> This is the tree root node. Can occur anywhere in the tree. Only a subset of root nodes, child nodes refers to the xml document the document element node. </ td> </ tr>
<tr> <td> Element (element) </ td> <td> such a node for any element in the document. Element node child nodes can be the contents of the element node, comment node, processing nodes and text nodes information. </ td> </ tr>
<tr> <td> Text (text) </ td> <td> document all the text, are grouped into text nodes. Text nodes can not have the same text node immediately before or after the brother of the node. </ td> </ tr>
<tr> <td> Attribute (attribute) </ td> <td> each element node has a set of attributes attached to the node itself. Default attribute values to the same attribute with the specified manner. These nodes do not have child nodes. </ td> </ tr>
<tr> <td> Namespace (name) </ td> <td> For each a xlmns: and attributes at the beginning of the element node, the node has a name space. No child nodes of these nodes. </ td> </ tr>
<tr> <td> Processing Instruction (processing instructions) </ td> <td> each processing instruction node has a separate. These nodes do not have child nodes. </ td> </ tr>
<tr> <td> Comment (Notes) </ td> <td> each node has a note. These nodes do not 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 the language setting
/ / $ inXML = str_replace ( 'encoding = "GB2312 "',"",$ inXML);
$ inXML = eregi_replace ( 'encoding = "[a-z0-9_-]+"',"",$ inXML);

$ doc = xmltree ($ inXML); / / use xmltree Analysis
$ myxml = $ doc-> dumpmem (); / / convert the string, the first for the xml version = "1.0"
/ / If once, will become the first xml version = "1.0" encoding = "ISO-8859-1"
/ / $ myxml = eregi_replace ('&#[ 0-9 ]+;',"",$ myxml); / / delete
echo "with xmltree Analysis <br>";
echo "<textarea cols=60 rows=5> $ myxml </ textarea> <br>";
/ / print_r ($ doc); / / can see the whole tree can also be used var_dump ($ doc);

/ / Xmldoc
$ doc = xmldoc ($ inXML);
$ myxml = $ doc-> dumpmem ();
echo "by xmldoc Analysis <br>";
echo "<textarea cols=60 rows=5> $ myxml </ textarea> <br>";
/ / print_r ($ doc); / / only the root node

/ / Domxml_new_xmldoc
$ doc = domxml_new_xmldoc ( "1.0"); [AutoPage]

$ 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, the implementation of the superposition of many
domxml_node_set_content ($ head, "ttt");

/ / Function name is in line with only 1-2 "_" function, the method can be used as a target

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

/ / Node of the traversal
/ **
Node structure
DomElement Object
type = 1
tagname = node name
DomText Object
type = 3
content = section points
DomCData Object
type = 4
content = section points

DomProcessingInstruction Object
type without
target = processing instructions
data = parameters

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

/ / Function domxml_children () can not go back to the node parameters
/ / Return the node parameters 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 = "";
/ / Access node, stored in the array
if (get_class ($ xmldoc) == "DomDocument") (
$ xmlstr = '<? xml version = "1.0" encoding = "gb2312 "?>'." \ n";
if (count ($ xmldoc-> children) == 1) / / Root, not the other members of
$ docs [] = $ xmldoc-> root ();
else
$ docs = $ xmldoc-> children (); / / root, there are other members of
) 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 the first
if ($ attr) (
foreach ($ attr as $ key)
$ xmlstr .= "($ key-> name) = \" ($ key-> value) \ ""; / / tag parameters
)
$ xmlstr .= ">"; / / label the end of
$ xmlstr .= xml_dumpmem ($ doc); / / to enter the sub-node
$ xmlstr .= "</{$ doc-> tagname)> "; / / closing tag
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); / / use xmltree Analysis
$ doc = xmldoc ($ inXML); / / use xmldoc Analysis [AutoPage]
)

/ / Can not be used to resolve xsl document

$ myxml = xml_dumpmem ($ doc);
echo "to write a wrong dumpmem not <br>";
echo "<textarea cols=60 rows=5> $ myxml </ textarea> <br>";
print_r ($ doc);

?>
</ pre>


Php XML Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.