简体   繁体   English

PHP:SimpleXMLElement 不解析链接标签的 href 属性

[英]PHP: SimpleXMLElement dose not parse href attributes of link tag

I want to parse xxm file like the below.我想解析 xxm 文件,如下所示。 but the result does not have any attributes include href for first "a" tag.但结果没有任何属性,包括第一个“a”标签的 href。

<?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
                            )

                    )

            )

    )

You can't reliably use print_r (or var_dump , etc) to inspect a SimpleXML element.您不能可靠地使用print_r (或var_dump等)来检查 SimpleXML 元素。 The output can be missing a lot of the values. output 可能会丢失很多值。 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.但是只需使用 API 进入 object,值就会在那里。 If you want the href value of the first link, it's here:如果你想要第一个链接的href值,它在这里:

$xml->li[0]->a['href'];

// Untitled-1-1.xhtml

See https://3v4l.org/BOVfBhttps://3v4l.org/BOVfB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM