Classes in PHP-manipulating XML (3)

Source: Internet
Author: User
Tags cdata closing tag constructor tag name what array

/* Here are the XML functions needed by expat * *


/* When expat hits a opening tag, it fires up this function * *

function startelement ($parser, $name, $attrs) {

Array_push ($this->current_tag, $name); Add tag to the cur. Tag array

$curtag = Implode ("_", $this->current_tag); Piece together Tag

/* This tracks what array index we are in for this tag */

if (Isset ($this->tagtracker["$curtag"])) {
$this->tagtracker["$curtag"]++;
} else {
$this->tagtracker["$curtag"]=0;
}


/* If there are attributes for this tag, we have set them here. */

if (count ($attrs) >0) {
$j = $this->tagtracker["$curtag"];
if (! $j) $j = 0;

if (!is_object ($GLOBALS [$this->identifier]["$curtag"] [$j])) {
$GLOBALS [$this->identifier]["$curtag"] [$j] = new Xml_container;
}

$GLOBALS [$this->identifier]["$curtag"] [$j]->store ("Attributes", $attrs);
}

}//End Function startelement



/* When expat hits a closing tag, it fires the up this function * *

function EndElement ($parser, $name) {

$curtag = Implode ("_", $this->current_tag); Piece together Tag
Before we pop it off,
So we can get the correct
Cdata

if (! $this->tagdata["$curtag"]) {
$popped = Array_pop ($this->current_tag); Or else we screw up where we are
Return If we have no data for the tag
} else {
$TD = $this->tagdata["$curtag"];
unset ($this->tagdata["$curtag");
}

$popped = Array_pop ($this->current_tag);
We want the tag name for
The tag above this, it
Allows us to group the
Tags together in a
Intuitive Way.

if (sizeof ($this->current_tag) = = 0) return; If we aren ' t in a tag

$curtag = Implode ("_", $this->current_tag); Piece together Tag
This time is for the arrays

$j = $this->tagtracker["$curtag"];
if (! $j) $j = 0;

if (!is_object ($GLOBALS [$this->identifier]["$curtag"] [$j])) {
$GLOBALS [$this->identifier]["$curtag"] [$j] = new Xml_container;
}

$GLOBALS [$this->identifier]["$curtag"] [$j]->store ($name, $TD); # $this->tagdata["$curtag"]);
Unset ($TD);
return TRUE;
}



/* When Expat finds some internal tag character data,
It fires up this function * *

function Characterdata ($parser, $cdata) {
$curtag = Implode ("_", $this->current_tag); Piece together Tag
$this->tagdata["$curtag"]. = $cdata;
}


/* This is the constructor:automatically called then the class is initialized * *

function XML ($data, $identifier = ' xml ') {

$this->identifier = $identifier;

Create parser Object
$this->xml_parser = Xml_parser_create ();

Set up some options and handlers
Xml_set_object ($this->xml_parser, $this);
Xml_parser_set_option ($this->xml_parser,xml_option_case_folding,0);
Xml_set_element_handler ($this->xml_parser, "startelement", "endelement");
Xml_set_character_data_handler ($this->xml_parser, "Characterdata");

if (!xml_parse ($this->xml_parser, $data, TRUE)) {
sprintf ("XML error:%s at line%d")
Xml_error_string (Xml_get_error_code ($this->xml_parser)),
Xml_get_current_line_number ($this->xml_parser));
}

We are done with the parser
Xml_parser_free ($this->xml_parser);

}//End constructor:function XML ()


}//Thus, we end class XML

?>



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.