我正在处理巨大的XML文件(数百MB)。我找到了适合我的完美解决方案: How to use XMLReader in PHP?(接受回答)。
现在,在我的代码中,我需要知道当前处理的节点的路径。我正在使用DOM函数getNodePath。除非XML文档已声明默认NS,否则一切看起来都不错。
这是我的代码:
$doc = new DOMDocument();
$z = new XMLReader;
$z->open($_SERVER['argv'][1]);
while ($z->read() && $z->name !== 'header');
$onix = simplexml_import_dom($doc->importNode($z->expand(), true));
$xpath = dom_import_simplexml($onix)->getNodePath();
echo "#".$xpath."#".PHP_EOL;
这里是示例文件。首先 - 工作 - 没有声明NS,第二个是命名空间。
<ONIXmessage>
<header>
<m174>blah</m174>
</header>
</ONIXmessage>
<ONIXmessage xmlns="http://www.editeur.org/onix/2.1/short">
<header>
<m174>blah</m174>
</header>
</ONIXmessage>
我需要得到&#34; / header&#34;在我的代码中。但我得到&#34; / *&#34;如果我解析第二个文件。 使用字符串函数删除xmlns属性有效,但我无法接受。