PHP - 从 XML 内容获取值
PHP - Get values from XML content
见下文,XML body 我有:
<?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 & Price。
我试着这样做:
$data = simplexml_load_string($xml),但是当我执行 print_r($data->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
)
)
)
但我希望有:
OX11213001-M61
136.36
在“内容”部分。
那么,我怎样才能得到这些值呢?以及如何正确地在这些所有“条目”之间进行循环?
我试过 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
见下文,XML body 我有:
<?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 & Price。
我试着这样做: $data = simplexml_load_string($xml),但是当我执行 print_r($data->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
)
)
)
但我希望有:
那么,我怎样才能得到这些值呢?以及如何正确地在这些所有“条目”之间进行循环?
我试过 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