A simple PHP class for parsing XML documents

Source: Internet
Author: User
Tags count empty modify php class tagname
Xml

Original, free to use, welcome suggestions for improvement,

? Php
Elements in the XML
Class Xmltag
{
var $parent;//Parent node
var $child//child node
var $attribute//This node property
var $data//This node data
var $TagName//This node name
var $depth//The depth of this node, the root node is 1
function Xmltag ($tag = ')
{
$this->attribute = Array ();
$this->child = Array ();
$this->depth = 0;
$this->parent = null;
$this->data = ';
$this->tagname = $tag;
}
function Settagname ($tag)
{
$this->tagname = $tag;
}
Function setparent (& $parent)
{
$this->parent = & $parent;
}
function setattribute ($name, $value)
{
$this->attribute[$name] = $value;
}
Function appendchild (& $child)
{
$i = count ($this->child);
$this->child[$i] = & $child;
}
function SetData ($data)
{
$this->data= $data;
}
function GetAttr ()
{
return $this->attribute;
}
function GetProperty ($name)
{
return $this->attribute[$name];
}
function GetData ()
{
return $this->data;
}
function GetParent ()
{
return $this->parent;
}
function Getchild ()
{
return $this->child;
}
function Getchildbyname ($name)
{
$total = count ($this->child);
for ($i =0; $i < $total; $i + +)
{
if ($this->child[$i]->attribute[' name ' = = $name)
{
return $this->child[$i];
}
}
return null;
}
Get a tag node
function getElementsByTagName ($tag)
{
$vector = Array ();
$tree = & $this;
$this->_getelementbytagname ($tree, $tag, $vector);
return $vector;
}
function _getelementbytagname ($tree, $tag,& $vector)
{
if ($tree->tagname = = $tag) Array_push ($vector, $tree);
$total = count ($tree->child);
for ($i = 0; $i < $total; $i + +)
$this->_getelementbytagname ($tree->child[$i], $tag, $vector);
}
}
XML document parsing
Class XmlDoc
{
var $parser;//xml Parse pointer
The XML tree generated by Var $XMLTree;/
var $XMLFile//XML document to be parsed
var $XMLData;//XML data to be parsed
var $error;//error message
var $NowTag;//node currently pointing to
var $TreeData;//Traverse generated XML tree wait for data
var $MaxDepth//The maximum depth of the tree
var $encode;//xml Document Encoding method
var $chs;//Character conversion
function xmldoc ()
{
Using the default iso-8859-1
$this->parser = Xml_parser_create ();
Xml_parser_set_option ($this->parser,xml_option_case_folding,0);
Xml_set_object ($this->parser,& $this);
Xml_set_element_handler ($this->parser, ' _startelement ', ' _endelement ');
Xml_set_character_data_handler ($this->parser, ' _cdata ');
$this->stack = Array ();
$this->xmltree = null;
$this->nowtag = null;
$this->maxdepth = 0;
}
function LoadFromFile ($file)
{
$this->xmlfile = fopen ($file, ' R ');
if (! $this->xmlfile)
{
$this->error = ' cannot open XML file ';
return false;
}
$this->xmldata = ';
$this->treedata = ';
return true;
}
function Setxmldata ($data)
{
if ($this->xmlfile) fclose ($this->xmlfile);
$this->xmldata = $data;
$this->treedata = ';
}
Add a new node to the tree
Function appendchild (& $child)
{
if ($this->xmltree = null)
{
$child->depth = 1;
$this->xmltree = & $child;
$this->nowtag = & $this->xmltree;
}
Else
{
$i = count ($this->nowtag->child);
$this->nowtag->child[$i] = & $child;
$child->parent = & $this->nowtag;
$child->depth = $this->nowtag->depth+1;
unset ($this->nowtag);
$this->nowtag = & $child;
}
$this->maxdepth = ($this->maxdepth < $this->nowtag->depth)? $this->nowtag->depth: $this-> MaxDepth;
}

To produce a new node
function &createelement ($tag)
{
$element = new Xmltag ($tag);
return $element;
}
function _startelement ($parser, $element, $attribute)
{
$tag = new Xmltag ();
$tag->tagname = $element;
$tag->attribute = $attribute;
if ($this->xmltree = null)
{
$tag->parent = null;
$tag->depth = 1;
$this->xmltree = & $tag;
$this->nowtag = & $tag;
}
Else
{
$i = count ($this->nowtag->child);
$this->nowtag->child[$i] = & $tag;
$tag->parent = & $this->nowtag;
$tag->depth = $this->nowtag->depth+1;
unset ($this->nowtag);
$this->nowtag = & $tag;
}
$this->maxdepth = ($this->maxdepth < $this->nowtag->depth)? $this->nowtag->depth: $this-> MaxDepth;
}
function _cdata ($paraser, $data)
{
$this->nowtag->data = $data;
}
function _endelement ($parser, $element)
{
$parent = & $this->nowtag->parent;
unset ($this->nowtag);
$this->nowtag = & $parent;
}
Start parsing XML document
Function Parse ()
{
if ($this->xmlfile)
{
$this->xmldata = ';
while (!feof ($this->xmlfile))
{
$this->xmldata. = fread ($this->xmlfile,4096);
}
}
Fclose ($this->xmlfile);
if ($this->xmldata)
{
$this->judgeencode ();
if (!xml_parse ($this->parser, $this->xmldata))
{
$code = Xml_get_error_code ($this->parser);
$col = Xml_get_current_column_number ($this->parser);
$line = Xml_get_current_line_number ($this->parser);
$this->error = "XML error: $col at line $line:". xml_error_string ($code);
return false;
}
}
Xml_parser_free ($this->parser);
return true;
}
Determine how to encode
function Judgeencode ()
{
$start = Strpos ($this->xmldata, ' <?xml ');
$end = Strpos ($this->xmldata, ' > ');
$str = substr ($this->xmldata, $start, $end-$start);
$pos = Strpos ($str, ' encoding ');
if ($pos!== false)
{
$str = substr ($str, $pos);
$pos = Strpos ($str, ' = ');
$str = substr ($str, $pos + 1);
$pos = 0;
while (Empty ($str [$pos])) $pos + +;
$this->encode = ';
while (!empty ($str [$pos]) && $str [$pos]!= '? ')
{
if ($str [$pos]!= ' "' && $str [$pos]!=" ")
$this->encode. = $str [$pos];
$pos + +;
}
}
$this->chs = new Chinese (' UTF-8 ', $this->encode);
}

Modify the value of a node based on the node name
function Changevaluebyname ($name, $name, $value)
{
return $this->_changevaluebyname ($this->xmltree, $name, $value);
}
function _changevaluebyname ($tree, $name, $value)
{
if (Is_array ($tree->attribute))
{
while (the list ($k, $v) = each ($tree->attribute))
{
if ($k = ' name ' && $v = $name)
{
$tree->data = $value;
return true;
}
}
}
$total = count ($tree->child);
for ($i = 0; $i < $total; $i + +)
{
$result = $this->_changevaluebyname ($tree->child[$i], $name, $value);
if ($result = = true) break;
}
return $result;
}

Modify the properties of a node in the tree based on the node name
function Changeattrbyname ($name, $attr, $value)
{
return $this->_changeattrbyname ($this->xmltree, $name, $attr, $value);
}
Function _changeattrbyname (& $tree, $name, $attr, $value)
{
if (Is_array ($tree->attribute))
{
while (the list ($k, $v) = each ($tree->atttibute))
{
if ($k = = ' name ' && $v = = $name)
{
$tree->attribute[$attr] = $value;
return true;
}
}
}
$total = count ($tree->child);
for ($i = 0; $i < $total; $i + +)
{
$result = $this->_changeattrbyname ($tree->child[$i], $name, $attr, $value);
if ($result = = true) break;
}
return $result;
}
Get root node
function Getdocumentelement ()
{
return $this->xmltree;
}
Iterate through the generated XML tree to regenerate the XML document
function Walktree ()
{
$this->treedata = ';
$this->_walktree ($this->xmltree);
return $this->treedata;
}
Recursive traversal
function _walktree ($tree)
{
$this->treedata. = ' < '. $tree->tagname. ' ';
if (Is_array ($tree->attribute))
{
while (the list ($key, $value) = each ($tree->attribute))
{
$this->treedata. = "$key =\" $value "";
}
}
$this->treedata. = ' > '. $tree->data;
$total = count ($tree->child);
for ($i =0; $i < $total; $i + +)
{
$this->_walktree ($tree->child[$i]);
}
$this->treedata. = ' </'. $tree->tagname. " >\n ";
}
Get error message
function GetError ()
{
return $this->error;
}
Get the maximum depth of a tree
function getmaxdepth ()
{
return $this->maxdepth;
}
Writing an XML tree to an XML file
function WriteToFile ($file, $head = ')
{
$fp = fopen ($file, ' w ');
if (! $fp)
{
$this->error = ' Unable to open write file ';
return false;
}
if (Empty ($this->treedata)) $this->walktree ();
$head = Empty ($head)? ' <?xml version= "1.0" standalone= "yes" encoding= "gb2312": $head;
Fwrite ($fp, $head);
Fwrite ($fp, $this->treedata);
Fclose ($FP);
return true;
}
}
?>



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.