Copy codeThe Code is as follows: <? Php
$ G_books = array ();
$ G_elem = null;
Function startElement ($ parser, $ name, $ attrs)
{
Global $ g_books, $ g_elem;
If ($ name = 'book') $ g_books [] = array ();
$ G_elem = $ name;
}
Function endElement ($ parser, $ name)
{
Global $ g_elem;
$ G_elem = null;
}
Function textData ($ parser, $ text)
{
Global $ g_books, $ g_elem;
If ($ g_elem = 'author' |
$ G_elem = 'her her '|
$ G_elem = 'title ')
{
$ G_books [count ($ g_books)-1] [$ g_elem] = $ text;
}
}
$ Parser = xml_parser_create ();
Xml_set_element_handler ($ parser, "startElement", "endElement ");
Xml_set_character_data_handler ($ parser, "textData ");
$ F = fopen ('books. xml', 'R ');
While ($ data = fread ($ f, 4096 ))
{
Xml_parse ($ parser, $ data );
}
Xml_parser_free ($ parser );
Foreach ($ g_books as $ book)
{
Echo $ book ['title']. "-". $ book ['author']. "-";
Echo $ book ['her her ']. "\ n ";
}
?>
Problems found in XML parsing using the SAX method in PHP
XML is as follows:
So. xmlCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "GBK"?>
<Result>
<Row>
<Id> 1047869 </id>
<Date> 14:54:51 </date>
<Title> the safflower still needs to be supported by green leaves-about the selection of the tripod cloud platform </title>
<Summary> many professional photographers often make great purchases when purchasing a tripod. 3. A 4000 yuan jettiser or a-yuan tripod is often bought without thinking about it. However, they always ignore the fine glare of the cloud platform. In fact, the stability of the digital camera rack on the tripod is unstable, and the decisive role is the cloud platform, so how can we choose a rock-solid cloud platform? The cloud platform family has a wide variety of uses. Simply put, the tripod cloud platform is a component used to connect the camera and the tripod for angle adjustment. It is mainly divided into three-dimensional cloud platform and ball cloud platform. Rotate the 3D cloud platform horizontally </summary>
</Row>
... (Omitted rows)
</Result>
Xml_class.phpCopy codeThe Code is as follows: <? Php
Class xml {
Var $ parser;
Var $ I = 0;
Var $ search_result = array ();
Var $ row = array ();
Var $ data = array ();
Var $ now_tag;
Var $ tags = array ("ID", "CLASSID", "SUBCLASSID", "CLASSNAME", "TITLE", "author title", "AUTHOR", "PRODUCER ", "SUMMARY", "CONTENT", "DATE ");
Function xml ()
{
$ This-> parser = xml_parser_create ();
Xml_set_object ($ this-> parser, $ this );
Xml_set_element_handler ($ this-> parser, "tag_open", "tag_close ");
Xml_set_character_data_handler ($ this-> parser, "cdata ");
}
Function parse ($ data)
{
Xml_parse ($ this-> parser, $ data );
}
Function tag_open ($ parser, $ tag, $ attributes)
{
$ This-> now_tag = $ tag;
If ($ tag = 'result '){
$ This-> search_result = $ attributes;
}
If ($ tag = 'row '){
$ This-> row [$ this-> I] = $ attributes;
}
}
Function cdata ($ parser, $ cdata)
{
If (in_array ($ this-> now_tag, $ this-> tags )){
$ Tagname = strtolower ($ this-> now_tag );
$ This-> data [$ this-> I] [$ tagname] = $ cdata;
}
}
Function tag_close ($ parser, $ tag)
{
$ This-> now_tag = "";
If ($ tag = 'row '){
$ This-> I ++;
}
}
}
?>
Search. phpCopy codeThe Code is as follows: <? Php
Require_once ("./xml_class.php ");
$ Xml = file_get_contents ("./so. xml ");
$ Xml_parser = new xml ();
$ Xml_parser-> parse ($ xml );
Print_r ($ xml_parser );
?>
In the final result, the data in the summary is much less, and the complete summary content is not always obtained. Sometimes garbled characters are returned, and I don't know what the problem is.
The problem was discovered later because xml_parser parses XML to process data in nodes cyclically. Each time, it takes about 300 characters in length (I am not sure about the specific length, it is only about 300 Output Using strlen), so it is known that the previous data is overwritten in each loop, so that the data is incomplete.
The solution is to set $ this-> data [$ this-> I] [$ tagname] = $ cdata IN THE cdata method of the xml class in the xml_class file; change to $ this-> data [$ this-> I] [$ tagname]. = $ cdata; (some of the NOTICE errors are ignored by PHP ).