分享到: ------解决方案-------------------- 嗯,SimpleXML、DOMDocument 都是一次性加载 xml 到内存
如果文件较大,可考虑使用 XML 语法解析函数
比如手册中的例子
$file = "data.xml";<br />
$depth = array();<br />
<br />
function startElement($parser, $name, $attrs)<br />
{<br />
global $depth;<br />
for ($i = 0; $i < $depth[$parser]; $i++) {<br />
echo " ";<br />
}<br />
echo "$name\n";<br />
$depth[$parser]++;<br />
}<br />
<br />
function endElement($parser, $name)<br />
{<br />
global $depth;<br />
$depth[$parser]--;<br />
}<br />
<br />
$xml_parser = xml_parser_create();<br />
xml_set_element_handler($xml_parser, "startElement", "endElement");<br />
if (!($fp = fopen($file, "r"))) {<br />
die("could not open XML input");<br />
}<br />
<br />
while ($data = fread($fp, 4096)) {<br />
if (!xml_parse($xml_parser, $data, feof($fp))) {<br />
die(sprintf("XML error: %s at line %d",<br />
xml_error_string(xml_get_error_code($xml_parser)),<br />
xml_get_current_line_number($xml_parser)));<br />
}<br />
}<br />
xml_parser_free($xml_parser);<br />
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