Reputation: 1540
I want to parse xxm file like the below. but the result does not have any attributes include href for first "a" tag.
<?php
$xmlContent = <<<XML
<ol>
<li>
<a href="Untitled-1-1.xhtml">1</a>
</li>
<li>
<a href="Untitled-1-2.xhtml"/>
</li>
</ol>
XML;
$xml = new \SimpleXMLElement($xmlContent);
print_r($xml);
?>
Result:
[li] => Array
(
[0] => SimpleXMLElement Object
(
[a] => 1
)
[1] => SimpleXMLElement Object
(
[a] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => Untitled-1-2.xhtml
)
)
)
)
Upvotes: 0
Views: 75
Reputation: 17434
You can't reliably use print_r
(or var_dump
, etc) to inspect a SimpleXML element. The output can be missing a lot of the values. See here if you want a more thorough explanation. Other tools are available for debugging these objects, if you do want a full view of them.
But just jump into the object using its API, and the values will be there. If you want the href
value of the first link, it's here:
$xml->li[0]->a['href'];
// Untitled-1-1.xhtml
Upvotes: 1