这里是XML:SimpleXMLElement文档 - 如何在SimpleXMLElement对象中返回子属性的值?
<Routes>
<Route type="source">
<Table>
<Tablename>incoming</Tablename>
<Fields>
<Fieldsname ref="description" name="Route Name">description</Fieldsname>
<Fieldsname name="CID Match">cidnum</Fieldsname>
<Fieldsname name="DID Match">extension</Fieldsname>
<Fieldsname ref="dest">destination</Fieldsname>
</Fields>
</Table>
</Route>
</Routes>
然后我在PHP它实例:
$doc = new SimpleXMLElement('routingConfig.xml', null, true);
print_r($doc->Route[0])
表明这一点:
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => source
)
[comment] => SimpleXMLElement Object
(
)
[Table] => SimpleXMLElement Object
(
[Tablename] => incoming
[comment] => SimpleXMLElement Object
(
)
[Fields] => SimpleXMLElement Object
(
[Fieldsname] => Array
(
[0] => description
[1] => cidnum
[2] => extension
[3] => destination
)
[comment] => Array
(
[0] => SimpleXMLElement Object
(
)
[1] => SimpleXMLElement Object
(
)
)
)
)
)
通知根值如何有@attributes
阵列。为什么$doc->Routes[0]->Table->Fields->Fieldsname
有@attributes
?我知道我可以通过attributes()
得到它,但是有没有办法让它包含在$doc
?
编辑 显然print_r()
不显示在阵列/对象的每价值,探索所有的孩子等等。也许SimpleXMLElement
不会返回它,除非请求(好像它都应该存放在$doc
) 。如果你这样做print_r($doc->Route[0]->Table->Fields->Fieldsname[0]);
,它返回
SimpleXMLElement Object
(
[@attributes] => Array
(
[ref] => description
[name] => Route Name
)
[0] => description
)
它显示我正在寻找的数据。但是,如果我做了print_r($doc->Route[0]->Table->Field);
数据不会出现。
'的print_r()旨在'用于调试。如果你确定你可以通过在这些节点上调用'attributes()'来获得attrs,为什么还要担心'print_r()'? –
它更是我想'$ doc'包含所有正确的价值观,包括'attributes'儿童,而不是担心'的print_r()'。我只是用'print_r'来显示'$ doc'对象/数组。 – Jared
你可以通过'simplexml_load_file()'加载它吗? –