PHP uses SAX to parse XML implementation code and Problem Analysis

Source: Internet
Author: User
Recently, I am working on a small program for parsing XML. Because the server is PHP4, XML parsing functions can only be parsed using xml_parser In the SAX method.

Recently, I am working on a small program for parsing XML. Because the server is PHP4, XML parsing functions can only be parsed using xml_parser In the SAX method.

The Code is as follows:
$ 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. xml
The Code is as follows:



1047869
2008-08-28 14:54:51
Safflower also needs green leaves to help-about the selection of the tripod Cloud Platform
Many professional photographers often make great purchases when purchasing a tripod. 3, 4000 yuan a gitzo or manfu tripod is often bought without thinking. 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

... (Omitted rows)


Xml_class.php
The Code is as follows:
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. php
The Code is as follows:
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 ).

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.