检索特定元素值XML - PHP

时间:2014-06-17 09:10:23

标签: 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 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文件中的结果代码。如何使用PHP获取<resultCode>的特定元素值?

3 个答案:

答案 0 :(得分:3)

DOMDocument与getElementsByTagName结合使用将是一个快速的解决方案。

DOMDocument::getElementsByTagName

$xml_string = '<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>';



$dom = new DOMDocument;
$dom->loadXML($xml_string);
$nodes = $dom->getElementsByTagName('resultCode');
foreach ($nodes as $node) {
    echo $node->nodeValue;
}

结果:P022

当然,还有其他几种在PHP中使用XML的方法(xpath等)

答案 1 :(得分:2)

使用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
}
?>

答案 2 :(得分:1)

实际上这对PHP很容易:

$resultCode = simplexml_load_string($xml_string)->xpath('//resultCode')[0];

var_dump((string) $resultCode); // string(4) "P022"

请注意,此代码没有错误检查,如果无法加载XML,可能会导致致命错误;如果您要查找的元素至少不存在一次,则可能会发出警告/通知。