PHP operation XML Full-featured XML operations Class _php tutorial

Source: Internet
Author: User
Tags addchild cdata
The 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 ()//Get node based on attribute name and value,
{//Parameters: Property Name, property value 1, property value 2, property 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)//Gets the node labeled $tag under the $node node
{
for ($i =0; $i children); $i)
{
if ($node->children[$i]->tag== $tag)
return $node->children[$i];
}
return NULL;
}

Public Function Getchildsbytag ($node, $tag)//Gets the node that is labeled $tag under the $node node and returns an array of node lists
{
$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 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 the $node 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 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 Srcnode to $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 ()//return 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 XML format file
{
$content = $this->__tostring ();

$fp = @fopen ($xmlFile, "w") or Die ("Create file 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

http://www.bkjia.com/PHPjc/486173.html www.bkjia.com true http://www.bkjia.com/PHPjc/486173.html techarticle The programming House provides a fully functional XML operation class?/* XML Operation class */class Operxml {var $parser; public Function __construct () {$this-parser = Xml_par Ser_create (); Xml_parser ...

  • Related Article

    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.