PHP-XML operations with complete functions-PHP Tutorial

Source: Internet
Author: User
Tags addchild
PHP has complete XML operation functions. Does the programming home provide a fully functional xml operation class? * Xml operation class * classoperXml {var $ parser; publicfunction _ construct () {$ this-parserxml_parser_create (); xml_parser programming House provides a fully functional xml operation class


/* Xml operation class */
Class operXml
{
Var $ parser;

Public function _ construct ()
{
$ This-> parser = xml_parser_create ();
Xml_parser_set_option ($ this-> parser, XML_OPTION_CASE_FOLDING, 0 );
Xml_parser_set_option ($ this-> parser, XML_OPTION_SKIP_WHITE, 1 );

Xml_set_object ($ this-> parser, $ this );
Xml_set_element_handler ($ this-> parser, "tag_open", "tag_close ");
Xml_set_character_data_handler ($ this-> parser, "cdata ");
}

Public function parse ($ xmlStr = "", $ endtag = true)
{
$ This-> endtag = $ endtag;
$ This-> xmlStr = $ xmlStr;
$ This-> tree = new stdClass;
$ This-> tree-> tag = "root ";
$ This-> tree-> props = new stdClass;
$ This-> tree-> children = array ();
$ This-> tree-> p = NULL;
$ This-> tree-> level =-1;
$ This-> deep = 0;
$ This-> pList = array ($ this-> tree );
Xml_parse ($ this-> parser, $ this-> xmlStr );
If (count ($ this-> tree-> children)> 0)
$ This-> root = $ this-> tree-> children [0];
Else
$ This-> root = NULL;
Return $ this;
}

Public function tag_open ($ parser, $ tag, $ attributes)
{
$ O = new stdClass;
$ O-> p = $ this-> pList [$ this-> deep];
$ O-> index = count ($ o-> p-> children );
$ O-> level = $ o-> p-> level 1;
$ O-> tag = $ tag;
$ O-> props = new stdClass;
While (list ($ key, $ value) = each ($ attributes ))
$ O-> props-> {$ key} = $ value;
$ O-> value = "";
$ O-> children = array ();
Array_push ($ o-> p-> children, $ o );
$ This-> deep;
$ This-> pList [$ this-> deep] = $ o;
}

Public function cdata ($ parser, $ cdata)
{
$ This-> pList [$ this-> deep]-> value = $ cdata;
}

Public function tag_close ($ parser, $ tag)
{
$ This-> deep --;
}

Public function getNodeByProp () // Obtain a node based on the attribute name and value,
{// Parameter: attribute name, attribute value 1, attribute value 2, attribute value 3 ,...
$ Args = func_get_args ();
$ Node = $ this-> tree;
For ($ I = 1; $ I {
$ Node = $ this-> _ getNodeByProp ($ node, $ args [0], $ args [$ I]);
If ($ node = NULL) break;
}
Return $ node;
}

Public function getChildByTag ($ node, $ tag) // Get the node labeled as $ tag under $ node
{
For ($ I = 0; $ I Children); $ I)
{
If ($ node-> children [$ I]-> tag = $ tag)
Return $ node-> children [$ I];
}
Return NULL;
}

Public function getChildsByTag ($ node, $ tag) // Get the node with the label $ tag under $ node and return the node list array
{
$ Rs = array ();
For ($ I = 0; $ I Children); $ I)
If ($ node-> children [$ I]-> tag = $ tag)
Array_push ($ rs, $ node-> children [$ I]);
Return $ rs;
}

Public function addRoot ($ tag) // add a root node
{
$ This-> tree-> children = array ();
$ This-> root = $ this-> addChild ($ this-> tree, $ tag );
Return $ this-> root;
}

Public function addChild ($ node, $ tag) // add a node labeled $ tag under $ node and return the added node
{
$ O = new stdClass;
$ O-> p = $ node;
$ O-> level = $ node-> level 1;
$ O-> index = count ($ node-> children );
$ O-> tag = $ tag;
$ O-> props = new stdClass;
$ O-> value = "";
$ O-> children = array ();
Array_push ($ node-> children, $ o );
Return $ o;
}

Public function delete ($ node) // delete $ node
{
$ P = $ node-> p;
Array_splice ($ p-> children, $ node-> index, 1 );
For ($ I = 0; $ I Children); $ I)
$ P-> children [$ I]-> index = $ I;
}

Public function move ($ dstNode, $ srcNode) // move the srcNode to the lower of $ dstNode
{
$ This-> delete ($ srcNode );
$ SrcNode-> p = $ dstNode;
$ SrcNode-> level = $ dstNode-> level 1;
$ SrcNode-> index = count ($ dstNode-> children );
Array_push ($ dstNode-> children, $ srcNode );
}

Public function _ toString () // returns the xml format string
{
$ S = "";
For ($ I = 0; $ I Tree-> children); $ I)
$ S. = $ this-> traversalNodeToXml ($ this-> tree-> children [$ I], $ this-> endtag );
Return $ s;
}

Public function save ($ xmlFile) // save as an xml file
{
$ Content = $ this->__ toString ();

$ Fp = @ fopen ($ xmlFile, "w") or die ("file creation failed:". $ xmlFile );
@ Fwrite ($ fp, $ content );
@ Fclose ($ fp );
@ Chmod ($ xmlFile, 0777 );
}

Private function traversalNodeToXml ($ treeNode, $ endtag)
{
$ Space = "";
$ Space = str_pad ($ s, $ treeNode-> level * 2, "", STR_PAD_LEFT );
$ S = $ space. "<". $ treeNode-> tag;
While (list ($ key, $ value) = each ($ treeNode-> props ))
$ S. = "$ key =" $ value "";
$ ChildCount = count ($ treeNode-> children );
If ($ childCount = 0)
{
If ($ treeNode-> value! = "" | $ Endtag)
$ S. = ">". $ treeNode-> value ." Tag. "> ";
Else
$ S. = "/> ";
Return $ s;
}

$ S. = "> ";
For ($ I = 0; $ I <$ childCount; $ I)
$ S. = $ this-> traversalNodeToXml ($ treeNode-> children [$ I], $ endtag );
$ S. = $ space ." Tag. "> ";
Return $ s;
}

Private function _ getNodeByProp ($ node, $ propName, $ propValue)
{
For ($ I = 0; $ I Children); $ I)
{
$ Anode = $ node-> children [$ I];
If (isset ($ anode-> props) & isset ($ anode->

Why? /* Xml operation class */class operXml {var $ parser; public function _ construct () {$ this-parser = xml_parser_create (); xml_parser...

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.