近日在做一个解析 代码如下:
$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 = $f = fopen( 'books.while( $data = fread( $f, 4096 ) )
{
}
foreach( $g_books as $book )
{
echo $book['TITLE']." - ".$book['AUTHOR']." - ";
echo $book['PUBLISHER']."\n";
}
?>
so. 代码如下:
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 {
$this->parser = parser, $this);
parser, "tag_open", "tag_close");
parser, "cdata");
}
function parse($data)
{
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. 代码如下:
最后得到的结果中summary中的数据少了很多,总是得不到完整的summary内容。有时还会得到乱码,在网上也找了半天也不知道是什么问题引起的。
后来才发现问题是因为 解决办法就是把data[$this->i][$tagname] = $cdata;改为$this->data[$this->i][$tagname] .= $cdata;即可解决(其中有一些NOTICE错误,