PHP parsing XML with SAX implementation code and problem Analysis _php tutorial

Source: Internet
Author: User
Tags cdata tagname
Copy CodeThe 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 = = ' PUBLISHER ' | |
$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 [' PUBLISHER ']. \ n ";
}
?>

parsing XML Discovery problems with sax in PHP
The XML is as follows:
So.xml
Copy CodeThe code is as follows:



1047869
2008-08-28 14:54:51
Safflower also need green leaf to help--on the purchase of tripod gimbal
Many professional photographers in the purchase of tripods, often generous, 3, 4000 yuan a letter or Manfrotto tripod often do not have to think to buy down, but they are always ignoring the precision of the gimbal is actually, digital camera rack on the tripod on the top of the stability, the decision is the gimbal, So how do we pick a rock-solid gimbal? Gimbal family a wide variety of uses very different simply said, the tripod gimbal is used to connect the camera and the tripod for angle adjustment of the components, mainly divided into three-dimensional gimbal and ball-shaped gimbal. Three-dimensional gimbal in transverse rotation

... (Omit several lines)


xml_class.php
Copy CodeThe 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", "Shorttitle", "AUTHOR", "PRODUCER", "SUMMARY", "CON TENT "," 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
Copy CodeThe 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 end, the data in summary is much less, and the complete summary content is not always available. Sometimes get garbled, also found on the internet for half a day do not know what the problem caused.
Later discovered that the problem is because Xml_parser parsing XML is the data in the Loop processing node, each time only take about 300 characters length (how much, I am not very clear, just with strlen output about 300), So I know because every time the cycle will be the previous data to the cover, so there will be incomplete data problems.
The solution is to $this->data[the CDATA method in the XML class in the Xml_class file $this->i][$tagname] = $cdata; instead $this->data[$this->i][$ TagName]. = $cdata; ok (there are some notice errors, PHP has been ignored).

http://www.bkjia.com/PHPjc/324133.html www.bkjia.com true http://www.bkjia.com/PHPjc/324133.html techarticle Copy the code as follows: PHP $g _books = Array (), $g _elem = null, function startelement ($parser, $name, $attrs) {Global $g _books, $g _elem; if ($name = = ' book ') $g _books []= ...

  • Related Article

    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.