我正在创建一个用于解析XML的类,并需要使用PHP中的xml_parse
方法。
它有三个参数,如下:
int xml_parse ( resource $parser , string $data [, bool $is_final = false ] )
根据PHP手册,is_final
表示是否为此解析发送的最后一段数据,但这是什么意思呢?这与resource $parser
有关吗?据我所知,此函数不允许输入数据流,因此我感到困惑。
请有人解释一下它的作用。
is_final
的含义是,如果你正在解析$data
的最后一行,你必须将此参数设置为true。
此外,文档中还有一条注释:
Entity errors are reported at the end of the parse. And will only show if the "end" parameter is TRUE
<?php
$parser=xml_parser_create();
function char($parser,$data)
{
echo $data;
}
xml_set_character_data_handler($parser,"char");
$fp=fopen("test.xml","r");
while ($data=fread($fp,4096))
{
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
?>
A document may be parsed piece-wise by calling xml_parse() several times with new data, as long as the is_final parameter is set and TRUE when the last data is parsed.
- splash58