Today we see a good class-operating xml! Post it to everyone. I don't know what I 've posted before.

Source: Internet
Author: User
Tags closing tag what array
? /* (C) 2000HansAndersonCorporation. AllRightsReserved. YouarefreetouseandmodifythisclassunderthesameguidelinesfoundinthePHPLicense. ----------- bugs/me: http://www.ha /*
2000 Hans Anderson Corporation. All Rights Reserved.
You 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 first actual release of the class. It's
Finally what I was hoping it wocould be, though there
Are likely to still be 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.

-. 92 fixed bug that didn't include tag attributes

(To use attributes, add _ attributes [array_index]
To the end of the tag in question:
$ Xml_html_head_body_img wocould become
$ Xml_html_head_body_img_attributes [0],
For example)

-- Thanks to Nick Winfield
For reporting this bug.

-. 91 No Longer requires PHP4!

-. 91 now all elements are array. Using objects has
Been discontinued.
*/

Class xml_container {

Function store ($ k, $ v ){
$ This-> {$ k} [] = $ v;
}
}


/* Parses the information */
/*********************************
* Class definition starts
*
*********************************/
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 an 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 on for this tag */

If (isset ($ this-> tagtracker ["$ curtag"]) {
$ This-> tagtracker ["$ curtag"] ++;
}
Else {
$ This-> tagtracker ["$ curtag"] = 0;
}

/* If there are attributes for this tag, we 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 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
// The tag above this, it
// Allows us to group
// Tags together in a more
// 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 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, so let's free it
Xml_parser_free ($ this-> xml_parser );

} // End constructor: function xml ()


} // Thus, we end our class xml

?>




Operation method:

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


Print $ xml ["hans"] [0]-> num_results [0];
For ($ I = 0; $ 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.