Php 在SimpleXmlElement上使用xPath获得单字符串结果
我正试图从谷歌的结账回复中获取一些数据。我使用SimpleXmlElement和xpath来搜索数据。其中一个问题是xpath的响应是一个SimpleXmlElements数组——我只需要字符串数据。另一个是响应可能包含标记名的多个LPE实例-例如,Php 在SimpleXmlElement上使用xPath获得单字符串结果,php,Php,我正试图从谷歌的结账回复中获取一些数据。我使用SimpleXmlElement和xpath来搜索数据。其中一个问题是xpath的响应是一个SimpleXmlElements数组——我只需要字符串数据。另一个是响应可能包含标记名的多个LPE实例-例如,至少显示两次,但我只想要一个结果 这是我到目前为止所拥有的 $notifyData = array(); $notifyData['buyerEmail'] = $data->xpath('buy
至少显示两次,但我只想要一个结果
这是我到目前为止所拥有的
$notifyData = array();
$notifyData['buyerEmail'] = $data->xpath('buyer-billing-address//email')[0];
$notifyData['buyerName'] = $data->xpath('buyer-billing-address//contact-name');
$notifyData['transactionId'] = $data->xpath('/new-order-notification/google-order-number');
$notifyData['itemName'] = $data->xpath('//item-name');
$notifyData['amount'] = $data->xpath('//unit-price');
$notifyData['currency'] = $notifyData['amount'][0]['currency'];//$data->xpath('//@currency[1]');
下面是print_r()对$notifyData的响应
Array
(
[buyerEmail] => Array
(
[0] => SimpleXMLElement Object
(
[0] => sandbox@mysite.com
)
)
[buyerName] => Array
(
[0] => SimpleXMLElement Object
(
[0] => Mr Sandbox Buyer
)
)
[transactionId] => Array
(
[0] => SimpleXMLElement Object
(
[0] => 271578474675716
)
)
[itemName] => Array
(
[0] => SimpleXMLElement Object
(
[0] => My Item
)
)
[amount] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => USD
)
[0] => 3.99
)
)
[currency] => SimpleXMLElement Object
(
[0] => USD
)
)
我想让数据在打印后显示为这样
下面是一个xml响应的示例
<new-order-notification xmlns="http://checkout.google.com/schema/2" serial-number="654578974677716-00001-7">
<buyer-billing-address>
<address1>19 sandbox st</address1>
<address2></address2>
<phone></phone>
<email>sandbox@mysite.com</email>
<company-name></company-name>
<contact-name>Mr Sandbox Buyer</contact-name>
<fax></fax>
<country-code>AU</country-code>
<city>Buyers Town</city>
<region>VIC</region>
<postal-code>3460</postal-code>
</buyer-billing-address>
<timestamp>2010-10-24T04:25:41.723Z</timestamp>
<google-order-number>298578974677716</google-order-number>
<shopping-cart>
<items>
<item>
<digital-content>
<key is-encrypted="true">PWWoqHo+FtfTi5vHmquOFTFNb4DwNjInAxkW89PLxtU=</key>
<description>Follow the instructions</description>
<url>http://mysite.com</url>
</digital-content>
<item-name>my product</item-name>
<item-description>my product</item-description>
<unit-price currency="USD">3.99</unit-price>
<quantity>1</quantity>
</item>
</items>
</shopping-cart>
<order-adjustment>
<merchant-codes />
<total-tax currency="USD">0.0</total-tax>
<adjustment-total currency="USD">0.0</adjustment-total>
</order-adjustment>
<buyer-id>634168749882822</buyer-id>
<buyer-marketing-preferences>
<email-allowed>true</email-allowed>
</buyer-marketing-preferences>
<buyer-shipping-address>
<address1>19 sandbox st</address1>
<address2></address2>
<phone></phone>
<email>sandbox@mysite.com</email>
<company-name></company-name>
<contact-name>Mr Sandbox Buyer</contact-name>
<fax></fax>
<country-code>AU</country-code>
<city>Buyers Town</city>
<region>VIC</region>
<postal-code>3460</postal-code>
</buyer-shipping-address>
<order-total currency="USD">3.99</order-total>
<fulfillment-order-state>NEW</fulfillment-order-state>
<financial-order-state>REVIEWING</financial-order-state>
</new-order-notification>
沙盒街19号
sandbox@mysite.com
沙盒买家先生
金
买家镇
维克
3460
2010-10-24T04:25:41.723Z
298578974677716
PWWoqHo+FtfTi5vHmquOFTFNb4DwNjInAxkW89PLxtU=
按照说明去做
http://mysite.com
我的产品
我的产品
3.99
1.
0
0
634168749882822
真的
沙盒街19号
sandbox@mysite.com
沙盒买家先生
金
买家镇
维克
3460
3.99
新的
回顾
使用类型转换:
$notifyData['buyerName']=(字符串)$data->xpath('buyer-billing-address//contact-name') 这里不一定需要xpath。例如
$notifyData = array(
'buyerEmail' => (string)$data->{'buyer-billing-address'}->email[0],
'buyerName' => (string)$data->{'buyer-billing-address'}->{'contact-name'}[0],
'transactionId' => (string)$data->{'google-order-number'}[0]
);
var_dump($notifyData);
function getData() {
return <<< eox
<new-order-notification xmlns="http://checkout.google.com/schema/2" serial-number="654578974677716-00001-7">
...
</new-order-notification>
eox;
}
而且,通过via,您还可以处理多个元素,例如
$notifyData = array(
'buyerEmail' => (string)$data->{'buyer-billing-address'}->email[0],
'buyerName' => (string)$data->{'buyer-billing-address'}->{'contact-name'}[0],
'transactionId' => (string)$data->{'google-order-number'}[0]
);
var_dump($notifyData);
function getData() {
return <<< eox
<new-order-notification xmlns="http://checkout.google.com/schema/2" serial-number="654578974677716-00001-7">
...
</new-order-notification>
eox;
}
array(3) {
["buyerEmail"]=>
string(18) "sandbox@mysite.com"
["buyerName"]=>
string(16) "Mr Sandbox Buyer"
["transactionId"]=>
string(15) "298578974677716"
}