PHP -从XML内容中获取值



看下面,我有XML正文:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://start.exactonline.be/api/v1/393999/sync/logistics/" 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">
<title type="text">SalesItemPrices</title>
<link rel="self" title="SalesItemPrices" href="SalesItemPrices" />
<entry>
<id>https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117417L)</id>
<title type="text"></title>
<updated>2021-08-11T11:47:22Z</updated>
<author>
<name />
</author>
<link rel="edit" title="SalesItemPriceSync" href="SalesItemPrices(2781117417L)" />
<category term="Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:ItemCode>OX11213001-M61</d:ItemCode>
<d:Price m:type="Edm.Double">136.36</d:Price>
</m:properties>
</content>
</entry>
<entry>
<id>https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117419L)</id>
<title type="text"></title>
<updated>2021-08-11T11:47:22Z</updated>
<author>
<name />
</author>
<link rel="edit" title="SalesItemPriceSync" href="SalesItemPrices(2781117419L)" />
<category term="Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:ItemCode>EK1117005.58</d:ItemCode>
<d:Price m:type="Edm.Double">164.46</d:Price>
</m:properties>
</content>
</entry>
</feed>

我需要得到值:ItemCode &价格

我试着这样做:$data =simplexml_load_string($xml)entry)时,得到的结果如下:

SimpleXMLElement Object
(
[id] => https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117417L)
[title] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => text
)
)
[updated] => 2021-08-11T13:47:01Z
[author] => SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
)
)
[link] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => edit
[title] => SalesItemPriceSync
[href] => SalesItemPrices(2781117417L)
)
)
[category] => SimpleXMLElement Object
(
[@attributes] => Array
(
[term] => Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync
[scheme] => http://schemas.microsoft.com/ado/2007/08/dataservices/scheme
)
)
[content] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => application/xml
)
)
)

但我期望有:& lt; d: ItemCode> OX11213001-M61 lt; d:价格m: type ="Edm.Double"在136.36 & lt;/d: Price>在"内容"中部分。

那么,如何得到这些值呢?以及如何正确地在这些"条目"之间建立一个循环?

I try foreach($products->entry as $product)警告:为foreach()提供无效参数

下面似乎可以工作。请注意,您的元素是名称空间-d,并且有一个前缀。

$simpleXML = new SimpleXMLElement($xml);
$myResult  = [];
foreach($simpleXML->entry as $entry) {
$properties = $entry->content->children('m', true);
$myResult[] = ['itemCode' => (string) $properties->children('d', true)->ItemCode, 'itemPrice' => (string) $properties->children('d', true)->Price];
}
print_r($myResult); // to see the values 

最新更新