属性可以隐藏在 simplexml 对象中吗?

发布于 2024-10-08 15:55:08 字数 7489 浏览 1 评论 0原文

我有这个 simplexml 对象,它看起来不错,但是当我转换它的一部分时......例如: $xmlMeshHeading = $MeshHeading->asXML();

我突然获得了在简单 XML 对象中找不到的属性...? 以下是这两个文件的一些部分:

SimpleXMLObject:

[MeshHeadingList] => SimpleXMLElement Object [DescriptorName] => Acoustic Stimulation 
                                (
                                    [MeshHeading] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (

                                                    [QualifierName] => methods
                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Adolescent
                                                )

                                            [2] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Age Factors
                                                )

                                            [3] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Child
                                                )

                                            [4] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Electromyography
                                                    [QualifierName] => methods
                                                )

                                            [5] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Female
                                                )

                                            [6] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Galvanic Skin Response
                                                    [QualifierName] => physiology
                                                )

                                            [7] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Humans
                                                )

                                            [8] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Male
                                                )

                                            [9] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Muscle, Skeletal
                                                    [QualifierName] => physiology
                                                )

                                            [10] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Probability
                                                )

                                            [11] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Reaction Time
                                                    [QualifierName] => physiology
                                                )

                                            [12] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Sex Factors
                                                )

                                            [13] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Startle Reaction
                                                    [QualifierName] => physiology
                                                )

这里的 asXML 文件来自同一来源...:

 <MeshHeadingList>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Acoustic Stimulation</DescriptorName>
                <QualifierName MajorTopicYN="N">methods</QualifierName>
            </MeshHeading>

            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Adolescent</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Age Factors</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Child</DescriptorName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Electromyography</DescriptorName>
                <QualifierName MajorTopicYN="Y">methods</QualifierName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Female</DescriptorName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Galvanic Skin Response</DescriptorName>
                <QualifierName MajorTopicYN="N">physiology</QualifierName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Humans</DescriptorName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Male</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Muscle, Skeletal</DescriptorName>
                <QualifierName MajorTopicYN="Y">physiology</QualifierName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Probability</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Reaction Time</DescriptorName>
                <QualifierName MajorTopicYN="N">physiology</QualifierName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Sex Factors</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Startle Reaction</DescriptorName>
                <QualifierName MajorTopicYN="Y">physiology</QualifierName>

            </MeshHeading>
        </MeshHeadingList>

我的问题是... 1. SimpleXML中有没有“取消隐藏”属性的函数? 或者 2. 我如何从 XML 文件中获取内容,例如属性和值...?

最好的, 泰斯

I have this simplexml object and it looks fine, but when i convert a part of it... like:
$xmlMeshHeading = $MeshHeading->asXML();

I suddenly get attributes which i can't find in the simple XML object...?
Here are some parts of the two files:

SimpleXMLObject:

[MeshHeadingList] => SimpleXMLElement Object [DescriptorName] => Acoustic Stimulation 
                                (
                                    [MeshHeading] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (

                                                    [QualifierName] => methods
                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Adolescent
                                                )

                                            [2] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Age Factors
                                                )

                                            [3] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Child
                                                )

                                            [4] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Electromyography
                                                    [QualifierName] => methods
                                                )

                                            [5] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Female
                                                )

                                            [6] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Galvanic Skin Response
                                                    [QualifierName] => physiology
                                                )

                                            [7] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Humans
                                                )

                                            [8] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Male
                                                )

                                            [9] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Muscle, Skeletal
                                                    [QualifierName] => physiology
                                                )

                                            [10] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Probability
                                                )

                                            [11] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Reaction Time
                                                    [QualifierName] => physiology
                                                )

                                            [12] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Sex Factors
                                                )

                                            [13] => SimpleXMLElement Object
                                                (
                                                    [DescriptorName] => Startle Reaction
                                                    [QualifierName] => physiology
                                                )

And here the asXML file it's from the same source...:

 <MeshHeadingList>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Acoustic Stimulation</DescriptorName>
                <QualifierName MajorTopicYN="N">methods</QualifierName>
            </MeshHeading>

            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Adolescent</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Age Factors</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Child</DescriptorName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Electromyography</DescriptorName>
                <QualifierName MajorTopicYN="Y">methods</QualifierName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Female</DescriptorName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Galvanic Skin Response</DescriptorName>
                <QualifierName MajorTopicYN="N">physiology</QualifierName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Humans</DescriptorName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Male</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Muscle, Skeletal</DescriptorName>
                <QualifierName MajorTopicYN="Y">physiology</QualifierName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Probability</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Reaction Time</DescriptorName>
                <QualifierName MajorTopicYN="N">physiology</QualifierName>

            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Sex Factors</DescriptorName>
            </MeshHeading>
            <MeshHeading>
                <DescriptorName MajorTopicYN="N">Startle Reaction</DescriptorName>
                <QualifierName MajorTopicYN="Y">physiology</QualifierName>

            </MeshHeading>
        </MeshHeadingList>

My question is...
1. Is there a function to "unhide" the attributes in SimpleXML?
OR
2. How can i get stuff from an XML file, like attributes and the values...?

Best,
Thijs

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

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

发布评论

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

评论(2

柠檬心 2024-10-15 15:55:08

是的。仅当访问特定节点时才返回属性。每个示例:

$xml = new SimpleXMLElement('<xml><foo bar="baz">hello world</foo></xml>');
print_r($xml);

输出:

SimpleXMLElement Object
(
    [foo] => hello world
)

虽然这样:

$xml = new SimpleXMLElement('<xml><foo bar="baz">hello world</foo></xml>');
print_r($xml->foo);

输出:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [bar] => baz
        )

    [0] => hello world
)

这可能是因为 SimpleXMLElement 的内部结构。与 PHP 中的许多内置对象(DOMDocument 等)一样,大多数属性在使用 print_r 时不会打印出来。对于 SimpleXML 属性,除非您访问节点,否则 print_r 不会显示它们,因为它将重载为 __get (某种程度)。

Yes. Attributes are only returned when accessing a specific node. Per example:

$xml = new SimpleXMLElement('<xml><foo bar="baz">hello world</foo></xml>');
print_r($xml);

Outputs:

SimpleXMLElement Object
(
    [foo] => hello world
)

While this:

$xml = new SimpleXMLElement('<xml><foo bar="baz">hello world</foo></xml>');
print_r($xml->foo);

Outputs:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [bar] => baz
        )

    [0] => hello world
)

It's probably because of the internal structure of SimpleXMLElement. Like many built-in objects in PHP (DOMDocument, etc.), most properties aren't printed out when using print_r on them. For SimpleXML attributes, print_r won't display them unless you access a node, because it will overload to __get (kind of).

撩起发的微风 2024-10-15 15:55:08

print_r() 根本不是检查 SimpleXMLElement 对象的正确工具。

如果您想知道其中的内容,只需对其使用 ->asXML() 并以 XML 形式读取它。尽管 print_r()var_dump() 未显示,但其节点和属性 100% 始终可用。

print_r() is simply not the right tool to inspect SimpleXMLElement objects.

If you want to know what's inside of one, just use ->asXML() on it and read it in XML. 100% of its nodes and attributes are always available despite not being displayed by print_r() or var_dump().

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