Home  >  Article  >  Backend Development  >  PHP uses SAX to parse XML implementation code and problem analysis_PHP tutorial

PHP uses SAX to parse XML implementation code and problem analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:25:18816browse

复制代码 代码如下:

$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";
}
?>

PHP中用SAX方式解析XML发现的问题
XML如下:
so.xml
复制代码 代码如下:




1047869
2008-08-28 14:54:51
红花还需绿叶扶--浅谈脚架云台的选购
很多专业摄影师在选购三脚架的时候,往往出手阔绰,3、4000元一个的捷信或者曼富图三脚架常常不用经过思考就买下来了,可是,他们却总是忽视了云台的精挑细眩其实,数码相机架在三脚架上面究竟稳不稳,起决定作用的是云台,那么我们如何才能挑选到一款稳如磐石的云台呢?云台家族种类繁多用途迥异简单的说,脚架云台是用于连接相机与脚架进行角度调节的部件,主要分成三维云台和球型云台。三维云台在横向旋转

...(省略若干行)


xml_class.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", "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
复制代码 代码如下:

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 never obtained. Sometimes I get garbled codes, and even after searching online for a long time, I still don’t know what is causing the problem.
Later I discovered that the problem was because xml_parser parsed XML and processed the data in the node in a loop, and only took about 300 characters in length each time (I don’t know the specific length, but I just used strlen to output about 300) , then I realized that it was because each cycle would overwrite the previous data, which would cause the problem of incomplete data.
The solution is to change $this->data[$this->i][$tagname] = $cdata; in the cdata method in the xml class in the xml_class file to $this->data[$ this->i][$tagname] .= $cdata; can be solved (there are some NOTICE errors, which PHP has ignored).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324133.htmlTechArticleCopy 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 []=...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn