programing

SOAP XML을 구문 분석하는 방법은 무엇입니까?

subpage 2023. 8. 7. 22:34
반응형

SOAP XML을 구문 분석하는 방법은 무엇입니까?

SOAP XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PaymentNotification xmlns="http://apilistener.envoyservices.com">
      <payment>
        <uniqueReference>ESDEUR11039872</uniqueReference>      
        <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>
        <postingDate>2010-11-15T15:19:45</postingDate>
        <bankCurrency>EUR</bankCurrency>
        <bankAmount>1.00</bankAmount>
        <appliedCurrency>EUR</appliedCurrency>
        <appliedAmount>1.00</appliedAmount>
        <countryCode>ES</countryCode>
        <bankInformation>Sean Wood</bankInformation>
  <merchantReference>ESDEUR11039872</merchantReference>
   </payment>
    </PaymentNotification>
  </soap:Body>
</soap:Envelope>

결제' 요소를 얻는 방법은 무엇입니까?

구문 분석을 시도합니다(PHP).

$xml = simplexml_load_string($soap_response);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//payment') as $item)
{
    print_r($item);
}

결과가 비어 있습니다 :( 올바른 구문 분석 방법이 있습니까?

네임스페이스 접두사를 처리하는 가장 간단한 방법 중 하나는 XML 응답을 다음과 같은 simplexml로 전달하기 전에 해당 접두사를 제거하는 것입니다.

$your_xml_response = '<Your XML here>';
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response);
$xml = simplexml_load_string($clean_xml);

그러면 다음이 반환됩니다.

SimpleXMLElement Object
(
    [Body] => SimpleXMLElement Object
        (
            [PaymentNotification] => SimpleXMLElement Object
                (
                    [payment] => SimpleXMLElement Object
                        (
                            [uniqueReference] => ESDEUR11039872
                            [epacsReference] => 74348dc0-cbf0-df11-b725-001ec9e61285
                            [postingDate] => 2010-11-15T15:19:45
                            [bankCurrency] => EUR
                            [bankAmount] => 1.00
                            [appliedCurrency] => EUR
                            [appliedAmount] => 1.00
                            [countryCode] => ES
                            [bankInformation] => Sean Wood
                            [merchantReference] => ESDEUR11039872
                        )

                )

        )

)

PHP 버전 > 5.0에는 멋진 SoapClient가 통합되어 있습니다.응답 xml을 구문 분석할 필요가 없습니다.여기 간단한 예가 있습니다.

$client = new SoapClient("http://path.to/wsdl?WSDL");
$res = $client->SoapFunction(array('param1'=>'value','param2'=>'value'));
echo $res->PaymentNotification->payment;

코드에서 다음을 쿼리하고 있습니다.payment기본 네임스페이스에 있는 요소이지만 XML 응답에서는 다음과 같이 선언됩니다.http://apilistener.envoyservices.com네임스페이스입니다.

네임스페이스 선언이 누락되었습니다.

$xml->registerXPathNamespace('envoy', 'http://apilistener.envoyservices.com');

이제 사용할 수 있습니다.envoyxpath 쿼리의 네임스페이스 접두사:

xpath('//envoy:payment')

전체 코드는 다음과 같습니다.

$xml = simplexml_load_string($soap_response);
$xml->registerXPathNamespace('envoy', 'http://apilistener.envoyservices.com');
foreach ($xml->xpath('//envoy:payment') as $item)
{
    print_r($item);
}

참고: 다음을 제거했습니다.soap사용하지 않는 것처럼 보이는 네임스페이스 선언(xpath 쿼리에서 네임스페이스 접두사를 사용하는 경우에만 유용함).

$xml = '<?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                  <soap:Body>
                    <PaymentNotification xmlns="http://apilistener.envoyservices.com">
                      <payment>
                        <uniqueReference>ESDEUR11039872</uniqueReference>
                        <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>
                        <postingDate>2010-11-15T15:19:45</postingDate>
                        <bankCurrency>EUR</bankCurrency>
                        <bankAmount>1.00</bankAmount>
                        <appliedCurrency>EUR</appliedCurrency>
                        <appliedAmount>1.00</appliedAmount>
                        <countryCode>ES</countryCode>
                        <bankInformation>Sean Wood</bankInformation>
                  <merchantReference>ESDEUR11039872</merchantReference>
                   </payment>
                    </PaymentNotification>
                  </soap:Body>
                </soap:Envelope>';
        $doc = new DOMDocument();
        $doc->loadXML($xml);
        echo $doc->getElementsByTagName('postingDate')->item(0)->nodeValue;
        die;

결과:

2010-11-15T15:19:45

먼저 XML을 필터링하여 개체로 구문 분석해야 합니다.

$response = strtr($xml_string, ['</soap:' => '</', '<soap:' => '<']);
$output = json_decode(json_encode(simplexml_load_string($response)));
var_dump($output->Body->PaymentNotification->payment);

이는 나중에 개체를 어레이로 해결해야 할 경우에도 매우 유용합니다. $array = json_decode(json_encode($responseXmlObject));

먼저, 변경 객체가 배열이 되도록 구문 분석하기 위해 XML을 필터링해야 합니다.

//catch xml
$xmlElement = file_get_contents ('php://input');
//change become array
$Data = (array)simplexml_load_string($xmlElement);
//and see
print_r($Data);

절대적인 xPath를 사용해 보는 것은 어떻습니까?

//soap:Envelope[1]/soap:Body[1]/PaymentNotification[1]/payment

또는 결제라는 것을 알고 있기 때문에 결제에서 직접 선택하십시오.

//soap:Envelope[1]/soap:Body[1]/PaymentNotification[1]/payment/*

언급URL : https://stackoverflow.com/questions/4194489/how-to-parse-soap-xml

반응형