最近,我尝试从API端点检索数据,并取得了成功。结果是一个XML-feed,试图处理它我发现我的结果数组中不存在其中一个元素。
PHP代码:
$xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOERROR);
$ns = $xml->getNamespaces(true);
$xml->entry[0]->children($ns['http://schemas.microsoft.com/ado/2007/08/dataservices/metadata']);
XML Feed:
<feed xml:base="https://start.exactonline.nl/api/v1/1480371/salesinvoice/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<entry>
<id>1</id>
<m:inline>
<feed>
<title type=text>title</title>
<id>2</id>
<link rel="self" href="somelink">
<entry> //the missing element
<content>
<m:properties>
<d:property>data</d:property>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</entry>
</feed>
饲料通常包含muliple&lt; entry&gt;根目录下的元素和每个条目可以包含多个&lt; entry&gt;元素也是。
我尝试在print_r()
$xml->entry[0]->children($ns['http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'];
导致以下结果:
(
[title] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => text
)
)
[id] => 2;
[link] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => self
[href] => somelink
)
)
不幸的是PHP代码没有拿起元素,我在那里做错了吗?