2014-06-17 2 views
0

나는 다음과 같은 XML 파일이 있습니다가져 오는 특정 요소 값의 XML - PHP

<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

getElementsByTagName과 함께 DOMDocument가 빠른 해결책이 될 것입니다.

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

이 XPath를 사용하여 PHP에서 XMLS (XPath는 등)

2

와 함께 작동하는 여러 가지 다른 방법이 있습니다. 여기 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 
} 
?> 
1

는 사실이 PHP와 매우 쉽습니다의 :이 코드는 오류 검사가 없습니다

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

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

하는 것으로이 경우에는 치명적인 오류, 그것은 수있는 XML 수 없습니다 찾고있는 요소가 적어도 한 번 이상 존재하지 않으면로드되고 경고 /주의 사항이 표시됩니다.

관련 문제