Copy Code 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 []= 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 ";
}
?>
the problem of parsing XML Discovery with sax in PHP
The XML is as follows:
So.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "GBK"?>
<result>
<row>
<id>1047869</id>
<date>2008-08-28 14:54:51</date>
<title> safflower also need green leaves to help--on the tripod platform to buy </title>
<summary> Many professional photographers are buying a tripod, often lavish, 3, 4000 yuan a Czech letter or a man-rich map tripod often do not have to think about it, but, they always ignore the fine glare of the cloud platform in fact, the digital camera stand on the tripod on the above is stable , how can we pick a rock-like platform when we decide to play the role of the PTZ? The Yuntai family has a wide variety of uses. Simply put, the tripod PTZ is used to connect the camera and the tripod to adjust the angle of the components, mainly divided into three-dimensional ptz and ball-type PTZ. Three-dimensional ptz in transverse rotation </summary>
</row>
... (Omit several lines)
</result>
xml_class.php
Copy Code code 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", "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 Code code 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 end, the data in summary is much less than the whole summary content. Sometimes will get garbled, on the internet also find a half-day also do not know what is causing the problem.
Later found that the problem is because Xml_parser parsing XML is a circular processing of data in the node, only take about 300 characters a length (specifically how much, I am not very clear, but with strlen output about 300), Then know that because each cycle will be the previous data to the cover, so there will be incomplete data problems.
The workaround is to $this->data[$this->i][in the CDATA method in the XML class in the Xml_class file $tagname] = $cdata; $this->data[$this->i][$ TagName] = $cdata; can be resolved (some notice errors, PHP ignored).