属性可以隐藏在 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 文件中获取内容,例如属性和值...?
最好的, 泰斯
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。仅当访问特定节点时才返回属性。每个示例:
输出:
虽然这样:
输出:
这可能是因为
SimpleXMLElement
的内部结构。与 PHP 中的许多内置对象(DOMDocument 等)一样,大多数属性在使用print_r
时不会打印出来。对于 SimpleXML 属性,除非您访问节点,否则print_r
不会显示它们,因为它将重载为__get
(某种程度)。Yes. Attributes are only returned when accessing a specific node. Per example:
Outputs:
While this:
Outputs:
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 usingprint_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).print_r()
根本不是检查SimpleXMLElement
对象的正确工具。如果您想知道其中的内容,只需对其使用
->asXML()
并以 XML 形式读取它。尽管print_r()
或var_dump()
未显示,但其节点和属性 100% 始终可用。print_r()
is simply not the right tool to inspectSimpleXMLElement
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 byprint_r()
orvar_dump()
.