? /* (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.
-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 */
/* 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
// 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"];
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.