0

PHPを使用して以下の結果から値を取得する方法。

<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Header/>
      <soapenv:Body>
        <p558:registerDonorResponse xmlns:p558="http://ws.ots.labcorp.com">
          <p558:registerDonorReturn xmlns:p118="http://data.ws.ots.labcorp.com">
            <p118:clientRegistrationId>clr1</p118:clientRegistrationId>
            <p118:labcorpRegistrationNumber>100059064</p118:labcorpRegistrationNumber>
            <p118:registrationTime>2012-12-01T05:40:51.628Z</p118:registrationTime>
          </p558:registerDonorReturn>
        </p558:registerDonorResponse>
      </soapenv:Body>
    </soapenv:Envelope>

ありがとう。

4

2 に答える 2

1

XML には、SOAP 応答で一般的な名前空間プレフィックス タグが含まれています。

PHPのSimpleXMLドキュメントから次のコメントを見てください:

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
    <p:person id="1">John Doe</p:person>
    <p:person id="2">Susie Q. Public</p:person>
</people>
XML;

$sxe = new SimpleXMLElement($xml);

$ns = $sxe->getNamespaces(true);

$child = $sxe->children($ns['p']);

foreach ($child->person as $out_ns)
{
    echo $out_ns;
}

あなたの場合、プロパティにアクセスするコードは次のようになります (so.xml ファイルの XML に対してテストされます)。

<?php
  $xml = file_get_contents('so.xml');
  $sxe = simplexml_load_string($xml);

  $ns = $sxe->getNamespaces(true);

  $child =
    $sxe->children($ns['soapenv'])->
      Body->children($ns['p558'])->
      registerDonorResponse->registerDonorReturn->children($ns['p118']);

  var_dump($child);

結果:

$ php -f so.php 
object(SimpleXMLElement)#4 (3) {
  ["clientRegistrationId"]=>
  string(4) "clr1"
  ["labcorpRegistrationNumber"]=>
  string(9) "100059064"
  ["registrationTime"]=>
  string(24) "2012-12-01T05:40:51.628Z"
}

ただし、SOAP リクエストを発行し、レスポンスを手動で解析することは、一般的に悪い習慣であることに注意してください。そのためにSOAP クライアントを使用することを検討してください。

于 2012-12-01T06:10:53.503 に答える
0

何を試しましたか?

simplexml_load_fileとにかく、PHPまたはフル機能を使用できますDOMDocument class

于 2012-12-01T06:16:21.830 に答える