0

だから私はこのXMLを解析したかった:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <requestContactResponse xmlns="http://webservice.foo.com">
      <requestContactReturn>
        <errorCode xsi:nil="true"/>
        <errorDesc xsi:nil="true"/>
        <id>744</id>
      </requestContactReturn>
    </requestContactResponse>
  </soapenv:Body>
</soapenv:Envelope>

具体的には、タグの値を取得したい<id>

これは私が試したものです:

$dom = new DOMDocument;
$dom->loadXML($xml);
$dom->children('soapenv', true)->Envelope->children('soapenv', true)->Body->children()->requestContactResponse->requestContactReturn->id;

しかし、私はこのエラーメッセージを受け取ります:

PHPの致命的なエラー:未定義のメソッドDOMDocument :: children()の呼び出し

また、simpleXMLを使用しようとしました。

$sxe = new SimpleXMLElement($xml);
$sxe->children('soapenv', true)->Envelope->children('soapenv', true)->Body->children()->requestContactResponse->requestContactReturn->id;

しかし、私はこの他のエラーメッセージを受け取ります:

PHPの致命的なエラー:非オブジェクトでのメンバー関数children()の呼び出し

私が試した最後の解決策:

$sxe = new SimpleXMLElement($xml);
$elements = $sxe->children("soapenv", true)->Body->requestContactResponse->requestContactReturn;

foreach($elements as $element) {
    echo "|-$element->id-|";
}

今回のエラーメッセージは次のとおりです。

Invalid argument supplied for foreach() 

助言がありますか?

4

1 に答える 1

1

ここで十分に文書化されていない事実は、で名前空間を選択すると->children子孫ノードに対して有効なままであるということです。

したがって$sxe->children("soapenv", true)->Body->requestContactResponse、SimpleXMLは、要求すると、まだ名前空間について話していると想定しているため、存在しない"soapenv"要素を探しています。<soapenv:requestContactResponse>

->childrenデフォルトのネームスペースに戻すには、ネームスペースを使用して再度呼び出す必要がありNULLます。

$sx->children("soapenv", true)->Body->children(NULL)->requestContactResponse->requestContactReturn->id
于 2012-08-20T22:37:02.510 に答える