2012-10-19 3 views
4
에서 PHP와 SOAP 응답을 구문 분석

가능한 중복 : 이제다른 방법

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <LoginResult xmlns="http://Siddin.ServiceContracts/2006/09">FE99E5267950B241F96C96DC492ACAC542F67A55</LoginResult> 
    </soap:Body> 
</soap:Envelope> 

내가 노력하고있어 :


How to parse SOAP response without SoapClient

내가 간단한 nuSoap의 XML 응답을 여기에 제안 된대로 simplexml_load_string으로 구문 분석 : parse an XML with SimpleXML which has multiple namespaces 및 여기 : Trouble Parsing SOAP response in PHP using simplexml,하지만 작동시키지 못합니다.

$xml = simplexml_load_string($this->getFullResponse()); 
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); 
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance'); 
$xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema'); 

foreach($xml->xpath('//soap:Body') as $header) { 
    var_export($header->xpath('//LoginResult')); 
} 

하지만 아직 결과 만이 얻을 : 내가 잘못을

/* array () 

를하고있는 중이 야 무엇

이 내 코드? 아니면 이해하기 쉽지 않은 간단한 일은 무엇입니까?


MySqlError하여 DOM과의 작업 최종 결과 :

$doc = new DOMDocument(); 
$doc->loadXML($response ); 
echo $doc->getElementsByTagName("LoginResult")->item(0)->nodeValue; 

ndm에 의해 SimpleXML을 가진 작업 최종 결과 :

$xml = simplexml_load_string($response); 
foreach($xml->xpath('//soap:Body') as $header) { 
    echo (string)$header->LoginResult; 
} 
+0

PHP DOMDocument를 http://php.net/manual/en/class.domdocument.php – MySqlError

+0

DOMDocument를 사용하는 대체 솔루션을 원하십니까? – MySqlError

+0

물론 시도해 볼 수 있습니다. – Peon

답변

15
$doc = new DOMDocument(); 
$doc->loadXML($yourxmlresponse); 

$LoginResults = $doc->getElementsByTagName("LoginResult"); 
$LoginResult = $LoginResults->item(0)->nodeValue; 

var_export($LoginResult); 
+0

이 결과가 없습니다. – Peon

+0

다시 확인하십시오. – MySqlError

+1

var_export를 변경하면 echo됩니다. return without mark var_export는 단지 문자열이라는 것을 보여주고 있습니다 – MySqlError

5

무엇을거야 여기서 잘못된 SimpleXMLs 기본 네임 스페이스 지원이 잘못되었습니다. XPath 식을 사용하여 해당 노드를 가져 오기 위해, 당신은 예를 들어, 요소가 접두사되지 않더라도, 기본 네임 스페이스 접두사를 등록하고 쿼리에서 사용할 필요 했어 그러나

foreach($xml->xpath('//soap:Body') as $header) { 
    $header->registerXPathNamespace('default', 'http://Siddin.ServiceContracts/2006/09'); 
    var_export($header->xpath('//default:LoginResult')); 
} 

실제로이 노드에 액세스하기위한 XPath를 사용할 필요가 없습니다, 당신은 단순히 직접 액세스 할 수 있습니다

foreach($xml->xpath('//soap:Body') as $header) { 
    var_export($header->LoginResult); 
} 
내가 제안
+0

이것은 어떤 식 으로든 작동하는 것처럼 보이지만 나에게 이런 식으로 문자열을줍니다. 마녀는 여전히 정확히 필요한 것이 아닙니다 :'SimpleXMLElement :: __ set_state (배열 ( 0 => 'FE99E5267950B241F96C96DC492ACAC542F67A55', )) ' – Peon

+0

글쎄, SimpleXML 노드의'var_export '가 어떻게 보이는지 예제 코드를 수정했다면, 값에 접근해야한다면 노드를 문자열로 변환하기 만하면된다.'(string) $ header-> LoginResult' – ndm

+0

Thanx m8, 너무 나쁘다 나는 두 가지 대답을 받아 들일 수 없다. – Peon