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

时间:2010-12-20 17:24:20

标签: php xml simplexml

我有这个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文件中获取内容,例如属性和值......?

最佳, 泰斯

2 个答案:

答案 0 :(得分:4)

是。仅在访问特定节点时返回属性。每个例子:

$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(种类)。

答案 1 :(得分:1)

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

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