这是 XML:
<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);
数据不会出现。