[英]Parsing XML-like document with PHP DOMDocument
I try to parse the XML-like structure of an EPUB index with PHP
DOMDocument
as 我尝试使用
PHP
DOMDocument
解析EPUB索引的类似XML的结构,如下所示:
$doc = new DOMDocument();
$xml = '
<navPoint playOrder="1" id="np-1">
<navLabel>
<text>I</text>
</navLabel>
<content src="/1.html"/>
</navPoint>
<navPoint playOrder="2" id="np-2">
<navLabel>
<text>II</text>
</navLabel>
<content src="/2.html"/>
</navPoint>
';
@$doc->loadHTML('<?xml encoding="utf-8" ?>
<html><head></head><body>' . $xml . '</body></html>');
$output = $doc->getElementsByTagName('navPoint');
print_r($output);
but it returns 但它返回
DOMNodeList Object
(
[length] => 0
)
What did I do wrong that it does not parse it as a straightforward HTML document? 我做错了什么,因为它没有将其解析为简单的HTML文档?
PS I tried PHP XML parser too, but as it is not an actual XML document, it gives errors because of an invalid XML. PS我也尝试过PHP XML解析器,但由于它不是实际的XML文档,因此由于XML无效而产生错误。 Therefore, I prefer to treat it as an HTML document.
因此,我更喜欢将其视为HTML文档。
You're looking for loadXML
, not loadHTML
. 您正在寻找
loadXML
,不loadHTML
。
No need to surround everything with HTML tags, just add a dummy <root>
item instead, because any valid XML document must have one (you can also add it to the $xml
variable itself). 不需要用HTML标记括起所有内容,只需添加一个虚拟
<root>
项,因为任何有效的XML文档都必须有一个(您也可以将其添加到$xml
变量本身)。
Also, using @
before function calls should be avoided in 99% cases, it prevents you from seeing/understanding what's wrong. 另外,在99%的情况下,应避免在函数调用之前使用
@
,这样可以防止您看到/了解问题所在。
The following should do it: 请执行以下操作:
$doc->loadXML('<root>' . $xml . '</root>');
Demo here: https://3v4l.org/s8QvM 演示在这里: https : //3v4l.org/s8QvM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.