检索特定元素值XML-PHP
我有以下xml文件:检索特定元素值XML-PHP,php,xml,Php,Xml,我有以下xml文件: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:doTransactionResponse xmlns:ns2="http://soap.api.controller.web.payjar.com/"> <return> <displayMessage>An error
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:doTransactionResponse xmlns:ns2="http://soap.api.controller.web.payjar.com/">
<return>
<displayMessage>An error occurred with this payment, please contact your merchant (ref: P022)</displayMessage>
<merchantReference>mercRef_1350047403</merchantReference>
<payUReference>11999149347</payUReference>
<pointOfFailure>PAYU</pointOfFailure>
<resultCode>P022</resultCode>
<resultMessage>Transaction is not in the correct state - last transaction: FINALIZE state: SUCCESSFUL</resultMessage>
<successful>false</successful>
</return>
</ns2:doTransactionResponse>
</soap:Body>
</soap:Envelope>
此付款发生错误,请联系您的商户(参考号:P022)
mercRef_1350047403
11999149347
帕尤
P022
事务未处于正确的状态-上一个事务:完成状态:成功
假的
我需要根据一系列代码测试xml文件中的结果代码。如何使用PHP获取
的特定元素值?DOMDocument与getElementsByTagName结合使用将是一个快速的解决方案
$xml\u string=>
此付款发生错误,请联系您的商户(参考号:P022)
mercRef_1350047403
11999149347
帕尤
P022
事务未处于正确的状态-上一个事务:完成状态:成功
假的
';
$dom=新的DOMDocument;
$dom->loadXML($xml\u字符串);
$nodes=$dom->getElementsByTagName('resultCode');
foreach($node作为$node){
echo$node->nodeValue;
}
结果:P022
当然,在PHP中使用XMLs还有其他几种方法(xpath等)。DOMDocument与getElementsByTagName结合使用将是一个快速的解决方案
$xml\u string=>
此付款发生错误,请联系您的商户(参考号:P022)
mercRef_1350047403
11999149347
帕尤
P022
事务未处于正确的状态-上一个事务:完成状态:成功
假的
';
$dom=新的DOMDocument;
$dom->loadXML($xml\u字符串);
$nodes=$dom->getElementsByTagName('resultCode');
foreach($node作为$node){
echo$node->nodeValue;
}
结果:P022
当然,在PHP中使用XML还有其他几种方法(xpath等)使用xpath。这是KURN提到的代码
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:doTransactionResponse
xmlns:ns2="http://soap.api.controller.web.payjar.com/">
<return>
<displayMessage>An error occurred with this payment, please contact
your merchant (ref: P022)</displayMessage>
<merchantReference>mercRef_1350047403</merchantReference>
<payUReference>11999149347</payUReference>
<pointOfFailure>PAYU</pointOfFailure>
<resultCode>P022</resultCode>
<resultMessage>Transaction is not in the correct state - last
transaction: FINALIZE state: SUCCESSFUL</resultMessage>
<successful>false</successful>
</return>
</ns2:doTransactionResponse>
</soap:Body>
</soap:Envelope>
';
$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach($xml->xpath('//soapenv:Body') as $header)
{
$arr = $header->xpath('//resultCode'); // Should output 'something'.
$resultCode = $arr[0];
echo $resultCode;// outputs P022
}
?>
使用xpath。这是KURN提到的代码
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:doTransactionResponse
xmlns:ns2="http://soap.api.controller.web.payjar.com/">
<return>
<displayMessage>An error occurred with this payment, please contact
your merchant (ref: P022)</displayMessage>
<merchantReference>mercRef_1350047403</merchantReference>
<payUReference>11999149347</payUReference>
<pointOfFailure>PAYU</pointOfFailure>
<resultCode>P022</resultCode>
<resultMessage>Transaction is not in the correct state - last
transaction: FINALIZE state: SUCCESSFUL</resultMessage>
<successful>false</successful>
</return>
</ns2:doTransactionResponse>
</soap:Body>
</soap:Envelope>
';
$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach($xml->xpath('//soapenv:Body') as $header)
{
$arr = $header->xpath('//resultCode'); // Should output 'something'.
$resultCode = $arr[0];
echo $resultCode;// outputs P022
}
?>
实际上,使用PHP很容易做到这一点:
$resultCode = simplexml_load_string($xml_string)->xpath('//resultCode')[0];
var_dump((string) $resultCode); // string(4) "P022"
请注意,此代码没有错误检查,如果无法加载XML,则可能会出现致命错误,如果您要查找的元素至少不存在一次,则会发出警告/通知。实际上,使用PHP很容易做到这一点:
$resultCode = simplexml_load_string($xml_string)->xpath('//resultCode')[0];
var_dump((string) $resultCode); // string(4) "P022"
请注意,此代码没有错误检查,如果无法加载XML,则可能会出现致命错误,如果要查找的元素至少不存在一次,则会发出警告/通知