See a good class today-manipulating XML! Posted out to everyone, do not know before the post did not?

Source: Internet
Author: User
Tags array cdata class definition closing tag end return tag name what array
XML;?
/*
(c) Hans Anderson Corporation. All Rights Reserved.
You are are free to use and modify this class under the same
Guidelines found in the PHP License.

-----------

Bugs/me:
http://www.hansanderson.com/php/
Me@hansanderson.com
Showstv@163.com

-----------

Version 1.0

-1.0 is the ' the ' ' actual release of the class. It ' s
Finally what I was hoping it would is, though there
are likely to still is some bugs in it. This is
A much changed version, and if you have downloaded
A previous version, this WON ' T work with your existing
scripts! You'll need to make some simple changes.

-. Didn fixed bug that ' t include tag attributes

(To use attributes, add _attributes[array_index]
To the "end of" the tag in question:
$xml _html_head_body_img would become
$xml _html_head_body_img_attributes[0],
For example)

--Nick Winfield <nick@wirestation.co.uk>
For reporting the This bug.

-No longer requires php4!

-All elements are array. Using Objects has
been discontinued.
*/

Class xml_container{

function Store ($k, $v) {
$this->{$k}[] = $v;
}
}


* * Parses the information * *
/*********************************
* Class definition begins
*
*********************************/
Class xml{

Initialize some variables
var $current _tag=array ();
var $xml _parser;
var $Version = 1.0;
var $tagtracker = 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;
}//End Function EndElement


/* 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;
}


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-class XML

?>




Action method:

Require (' class.xml.php ');
$file = "Data.xml";
$data = Implode ("", File ($file)) or die ("could not open XML input file");
$obj = new XML ($data, "xml");


Print $xml ["Hans"][0]->num_results[0];
For ($i =0 $i <sizeof ($xml ["Hans"]); $i + +) {
Print $xml ["Hans"] [$i]->tag[0]. " ";
}

To print URL attributes (if they exist):

Print $xml ["Hans"][0]->attributes[0]["size"];



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.