Reputation: 15683
I try to parse the XML-like structure of an EPUB index with PHP
DOMDocument
as
$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?
P.S. I tried PHP XML parser too, but as it is not an actual XML document, it gives errors because of an invalid XML. Therefore, I prefer to treat it as an HTML document.
Upvotes: 0
Views: 47
Reputation: 14927
You're looking for loadXML
, not 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).
Also, using @
before function calls should be avoided in 99% cases, it prevents you from seeing/understanding what's wrong.
The following should do it:
$doc->loadXML('<root>' . $xml . '</root>');
Demo here: https://3v4l.org/s8QvM
Upvotes: 2