php SimpleXML属性丢失 [英] php SimpleXML attributes are missing
问题描述
我在这里有以下xml文档: (请参阅下面的示例)
I have the following xml document here: (see below for sample)
我正在使用php/SimpleXML将其隐藏为对象以读取它:
I am using php/SimpleXML to covert it to an object to read it:
$xmlContent = file_get_contents($path . '/test.xml');
$tablesRaw = new SimpleXMLElement($xmlContent);
echo '<pre>';
print_r($tablesRaw);
echo '</pre>';
当我print_r时,我看到字段的属性,但是不显示可接受值的属性.这是原始xml的示例(我需要value属性):
When I print_r I see attributes for field but attributes for acceptable-value do not show. Here is an example of the raw xml (I need the value attribute):
<acceptable-value value="0">
Unknown
</acceptable-value>
这是我在print_r时看到的:
This is what I see when I print_r:
[acceptable-values] => SimpleXMLElement Object
(
[acceptable-value] => Array
(
[0] =>
Unknown
[1] =>
Invalid
[2] =>
Deleted
[3] =>
Valid/Good
[4] =>
Inactive
)
)
任何提示为何未显示属性?预先感谢.
Any clues why the attributes are not showing? Thanks in advance.
请求一些xml:
<field name="Address1Type" type="String"/>
<field name="Address2Street1" type="String"/>
<field name="Address2Street2" type="String"/>
<field name="Address2Type" type="String"/>
<field name="Address3Street1" type="String"/>
<field name="Status" type="Integer" access="R">
<acceptable-values>
<acceptable-value value="0">
Unknown
</acceptable-value>
<acceptable-value value="1">
Invalid
</acceptable-value>
<acceptable-value value="2">
Deleted
</acceptable-value>
<acceptable-value value="3">
Valid/Good
</acceptable-value>
<acceptable-value value="4">
Inactive
</acceptable-value>
</acceptable-values>
</field>
推荐答案
这里的简单答案是不要将print_r()
与SimpleXML对象一起使用.因为它们是非PHP数据的包装,所以通常会显示整个"对象的函数并不能真正反映出您正在查看的内容.
The simple answer here is not to use print_r()
with SimpleXML objects. Because they are wrappers around non-PHP data, functions like that which would normally show the "whole" object don't really reflect what you're looking at.
使用SimpleXML访问属性的方法是使用属性名称,就像它是数组键($node['attribute']
)一样;这不是不是,意味着在某个地方有一个带有该键的数组,这是伪装的函数调用.
The way to access an attribute with SimpleXML is to use the attribute name as though it was an array key ($node['attribute']
); this does not mean that there is an array somewhere with that key, it is a function-call in disguise.
如果您想在编写SimpleXML代码时了解正在查看的节点,请查看此<我写的c2>函数(欢迎反馈).
If you want to get a feel for which nodes you're looking at while writing SimpleXML code, check out this simplexml_dump()
function which I wrote (feedback welcome).
这篇关于php SimpleXML属性丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!