Send a class that uses php to read xml

Source: Internet
Author: User
Tags xml parser
Send a class that uses php to read xml. <? Php
###################################
#
# XML Library, by Keith Devens, version 1.2b
# <A href = "http://keithdevens.com/software/phpxml" target = "_ blank"> http://keithdevens.com/software/phpxml </a>
#
# This code is Open Source, released under terms similar to the Artistic License.
# Read the license at <a href = "http://keithdevens.com/software/license" target = "_ blank"> http://keithdevens.com/software/license </a>
#
########################################
# XML_unserialize: takes raw XML as a parameter (a string)
# And returns an equivalent PHP data structure
#####################################
Function & XML_unserialize (& $ xml ){
$ Xml_parser = & new XML ();
$ Data = & $ xml_parser-> parse ($ xml );
$ Xml_parser-> destruct ();
Return $ data;
}
#######################################
# XML_serialize: serializes any PHP data structure into XML
# Takes one parameter: the data to serialize. Must be an array.
###################################
Function & XML_serialize (& $ data, $ level = 0, $ prior_key = NULL ){
If ($ level = 0) {ob_start (); echo '<? Xml version = "1.0"?> ', "\ N ";}
While (list ($ key, $ value) = each ($ data ))
If (! Strpos ($ key, 'attr') # if it's not an attribute
# We don't treat attributes by themselves, so for an empty element
# That has attributes you still need to set the element to NULL

If (is_array ($ value) and array_key_exists (0, $ value )){
XML_serialize ($ value, $ level, $ key );
} Else {
$ Tag = $ prior_key? $ Prior_key: $ key;
Echo str_repeat ("\ t", $ level), '<', $ tag;
If (array_key_exists ("$ key attr", $ data) {# if there's an attribute for this element
While (list ($ attr_name, $ attr_value) = each ($ data ["$ key attr"])
Echo '', $ attr_name, '="', htmlspecialchars ($ attr_value ),'"';
Reset ($ data ["$ key attr"]);
}

If (is_null ($ value) echo "/> \ n ";
Elseif (! Is_array ($ value) echo '>', htmlspecialchars ($ value), "</$ tag> \ n ";
Else echo "> \ n", XML_serialize ($ value, $ level + 1), str_repeat ("\ t", $ level), "</$ tag> \ n ";
}
Reset ($ data );
If ($ level = 0) {$ str = & ob_get_contents (); ob_end_clean (); return $ str ;}
}
######################################## #
# XML class: utility class to be used with PHP's XML handling functions
###################################
Class XML {
Var $ parser; # a reference to the XML parser
Var $ document; # the entire XML structure built up so far
Var $ parent; # a pointer to the current parent-the parent will be an array
Var $ stack; # a stack of the most recent parent at each nesting level
Var $ last_opened_tag; # keeps track of the last tag opened.

Function XML (){
$ This-> parser = & xml_parser_create ();
Xml_parser_set_option (& $ this-> parser, XML_OPTION_CASE_FOLDING, false );
Xml_set_object (& $ this-> parser, & $ this );
Xml_set_element_handler (& $ this-> parser, 'open', 'close ');
Xml_set_character_data_handler (& $ this-> parser, 'data ');
}
Function destruct () {xml_parser_free (& $ this-> parser );}
Function & parse (& $ data ){
$ This-> document = array ();
$ This-> stack = array ();
$ This-> parent = & $ this-> document;
Return xml_parse (& $ this-> parser, & $ data, true )? $ This-> document: NULL;
}
Function open (& $ parser, $ tag, $ attributes ){
$ This-> data = ''; # stores temporary cdata
$ This-> last_opened_tag = $ tag;
If (is_array ($ this-> parent) and array_key_exists ($ tag, $ this-> parent) {# if you 've seen this tag before
If (is_array ($ this-> parent [$ tag]) and array_key_exists (0, $ this-> parent [$ tag]) {# if the keys are numeric
# This is the third or later instance of $ tag we 've come into your SS
$ Key = count_numeric_items ($ this-> parent [$ tag]);
} Else {
# This is the second instance of $ tag that we 've ve seen. shift around
If (array_key_exists ("$ tag attr", $ this-> parent )){
$ Arr = array ('0 attr' = >&$ this-> parent ["$ tag attr"], & $ this-> parent [$ tag]);
Unset ($ this-> parent ["$ tag attr"]);
} Else {
$ Arr = array (& $ this-> parent [$ tag]);
}
$ This-> parent [$ tag] = & $ arr;
$ Key = 1;
}
$ This-> parent = & $ this-> parent [$ tag];
} Else {
$ Key = $ tag;
}
If ($ attributes) $ this-> parent ["$ key attr"] = $ attributes;
$ This-> parent = & $ this-> parent [$ key];
$ This-> stack [] = & $ this-> parent;
}
Function data (& $ parser, $ data ){
If ($ this-> last_opened_tag! = NULL) # you don't need to store whitespace in between tags
$ This-> data. = $ data;
}
Function close (& $ parser, $ tag ){
If ($ this-> last_opened_tag ==$ tag ){
$ This-> parent = $ this-> data;
$ This-> last_opened_tag = NULL;
}
Array_pop ($ this-> stack );
If ($ this-> stack) $ this-> parent = & $ this-> stack [count ($ this-> stack)-1];
}
}
Function count_numeric_items (& $ array ){
Return is_array ($ array )? Count (array_filter (array_keys ($ array), 'is _ numeric '): 0;
}
?>

The following is an application instance where the XML source address is: http://data.cnaz.com/spread? Tid = 7 & sid = 42845 & order = date & flags = desc & num = 50 & page = 1 & hosts = pic.ttiankan.com & code = xml <? Php
Include ('xml. php'); // reference the php xml operation class

$ Page = $ _ GET ['page'];

If (empty ($ page) |! Is_numeric ($ page) $ page = 1;

// Idol stars
$ Xml = file_get_contents ('HTTP: // data.cnaz.com/spread /? Tid = 7 & sid = 42845 & order = date & flags = desc & num = 50 & page = '. $ page.' & hosts = pic.ttiankan.com & code = xml ');

// $ Xml = file_get_contents ("php: // input"); // read the input stream from POST
$ Data = XML_unserialize ($ xml );

$ Item = $ data ['rss '] ['channel'];

$ Link = $ item ['link'];
$ Description = $ item ['description'];
$ Keywords = $ item ['keyword'];
$ Totalnum = $ item ['totalnum'];
$ Usetime = $ item ['usetime'];

$ Totalpage = ceil ($ totalnum/50 );

Echo ('<div style = "width: 600px; margin: 5px auto; background: # eee; border: 1px # ccc solid; padding: 5px;"> Keyword :'. $ keywords. '');
Echo ('Total number of records :'. $ totalnum. ', 50 entries per page, a total '. $ totalpage. 'page, current is '. $ page. 'page </div> ');

$ Item = $ item ['item'];

Echo ('
<Style>
* {Font-size: 12px; color: #000; font-family: arial ;}
A {text-decoration: none ;}
A: hover {text-decoration: underline ;}
. Tbl {border-top: 1px # ccc solid; border-left: 1px # ccc solid ;}
. Tbl td {padding: 4px 5px; border-bottom: 1px # ccc solid; border-right: 1px # ccc solid ;}
</Style>
');

Echo ('
<Table cellspacing = "0" cellpadding = "0" border = "0" class = "tbl" width = "600">
<Tr>
<Td> Category </td>
<Td> title </td>
<Td> link </td>
</Tr>
');

Foreach ($ item as $ list)
{
Echo ('<tr> ');
Echo ('<td>'. $ list ['sort '].' </td> ');
Echo ('<td> <a title = "'. $ list ['description']. '"href = "'. $ list ['photo']. '"target =" _ blank "> '. $ list ['title']. '</a> </td> ');
Echo ('<td> <a href = "'. $ list ['link']. '"target =" _ blank "> '. $ list ['link']. '</a> </td> ');
Echo ('</tr> ');
}

Echo ('<tr>
<Td colspan = "3">
');
If ($ page> 1) echo ('<a href = "B. php? Page = '. ($ page-1).' "> previous page </a> ');
If ($ page <$ totalpage) echo ('<a href = "B. php? Page = '. ($ page + 1).' "> next page </a> ');
Echo ('</td> </tr> ');

Echo ('</table> ');

?>

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.