【发布时间】:2014-07-28 17:30:50
【问题描述】:
我阅读了以下 xml 文件:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<books uiTotalCount="45" uiTotalPages="2" >
<book title="Book1">
<registrationNumber formatCode="raw">BO1</registrationNumber>
</book>
<book title="Book2">
<registrationNumber formatCode="raw">BO2</registrationNumber>
</book>
</books>
使用以下代码时:
$xmlResponse = file_get_contents('\xml\books.xml', true);
xml = simplexml_load_string($xmlResponse);
var_dump($xml->book);
最后一行只给出第一个元素:
object(SimpleXMLElement)[3]
public '@attributes' =>
array (size=1)
'title' => string 'Book1' (length=5)
public 'registrationNumber' => string 'BO1' (length=3)`
当我查看 var_dump($xml) 时,我可以看到 2 本书:
object(SimpleXMLElement)[2]
public '@attributes' =>
array (size=2)
'uiTotalCount' => string '45' (length=2)
'uiTotalPages' => string '2' (length=1)
public 'book' =>
array (size=2)
0 =>
object(SimpleXMLElement)[3]
public '@attributes' =>
array (size=1)
...
public 'registrationNumber' => string 'BO1' (length=3)
1 =>
object(SimpleXMLElement)[4]
public '@attributes' =>
array (size=1)
...
public 'registrationNumber' => string 'BO2' (length=3)
有人知道我做错了什么吗?我想获得所有书籍的完整数组,以便我可以对其进行 jsonify。
【问题讨论】: