如何获取xml元素的值?

发布于 2024-09-01 11:36:48 字数 2104 浏览 0 评论 0原文

我有一些 xml 数据,我正在尝试访问一些元素。数据结构 如下(使用 print_r($data))。 我可以获得 $data->{'parent'}->title,它可以工作,但是如果我尝试使用获取 href 的值 $data->{'parent'}->link[0]->{'@attributes'}->href .. 它不起作用.. 有什么想法吗?

谢谢

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [children] => 29
            [modules] => 0
        )
[title] => Test title
[link] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [href] => data.php?id=2322
                        [rel] => self
                        [type] => application/xml
                    )

            )

        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [href] => data.php?id=2342
                        [rel] => alternate
                        [type] => text/html
                    )

            )

    )

[parent] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [children] => 6
                [modules] => 0
            )

        [title] => Top
        [link] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [href] => /data.php?id=5763
                                [rel] => self
                                [type] => application/xml
                            )

                    )

                [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [href] => /data.php?id=2342
                                [rel] => alternate
                                [type] => text/html
                            )

                    )

            )

    )

I have some xml data and I am trying to access some elements. The structure of data
is as below (using print_r($data)).
I can get $data->{'parent'}->title, it works but if I try to get value of href using
$data->{'parent'}->link[0]->{'@attributes'}->href .. it doesnt work .. any ideas?

Thanks

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [children] => 29
            [modules] => 0
        )
[title] => Test title
[link] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [href] => data.php?id=2322
                        [rel] => self
                        [type] => application/xml
                    )

            )

        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [href] => data.php?id=2342
                        [rel] => alternate
                        [type] => text/html
                    )

            )

    )

[parent] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [children] => 6
                [modules] => 0
            )

        [title] => Top
        [link] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [href] => /data.php?id=5763
                                [rel] => self
                                [type] => application/xml
                            )

                    )

                [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [href] => /data.php?id=2342
                                [rel] => alternate
                                [type] => text/html
                            )

                    )

            )

    )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-09-08 11:36:48

查看从 SimpleXML 访问@attribute,尤其是关于SimpleXML 对象的误导性 var_dump (print_r) 输出。

也就是说,IIRC 以下内容应该在您的示例中起作用:(

$data->{'parent'}->link[0]['href']

也就是说,可以使用标准数组表示法访问属性 - 这绝对适用于单个元素,不确定它是否适用于元素集合中的附加索引。)

Check out Accessing @attribute from SimpleXML, especially the comment on the misleading var_dump (print_r) output of SimpleXML Objects.

That said, IIRC the following should work in your example:

$data->{'parent'}->link[0]['href']

(That is, the attributes can be accessed using standard array notation - this definitely works on single elements, not sure if it works with the additional index into the element collection.)

伴我心暖 2024-09-08 11:36:48

不要使用 print_r() 来检查 SimpleXMLElement。相反,只需查看 XML。使用对象表示法 ->name 访问子项,使用数组表示法 ['name'] 访问属性。

在你的情况下,我想访问此属性的正确方法是

$data->parent->link[0]['href']

Do not use print_r() to inspect a SimpleXMLElement. Instead, just look at the XML. Children are accessed using the object notation ->name and attributes are accessed using the array notation ['name'].

In your case, I guess the correct way to access this attribute would be

$data->parent->link[0]['href']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文