4

他のサーバーから動的に値を取得する文字列があります。文字列の値は

$string1 = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.microsoft.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <AuthenticateUserResponse xmlns="http://microsoft.com/xml/namespace/2012/04">
      <Authenticator>AE1001</Authenticator>
    </AuthenticateUserResponse>
  </soap:Body>
</soap:Envelope>';

私の質問は、通常、*$xml = simplexml_load_file("test1.xml");* を使用して XML ファイルをロードしますが、ここで私の要件ではStringです。この String 値を読み取って子ノードとその子ノードを抽出するにはどうすればよいですか?価値?例:

<Authenticator> and its value "AE1001" ?

これを配列に入れる方法はありますか?ノード値を簡単に出力できるようにするには?

4

3 に答える 3

12

DOMDocumentXPathも学びます。それは本当に時間の価値があります。

$doc = new DOMDocument;
$doc->loadXML($string1);

echo $doc->getElementsByTagName('Authenticator')->item(0)->nodeValue; // AE1001
于 2012-05-22T17:43:09.160 に答える
1

http://www.php.net/manual/de/function.simplexml-load-string.php

http://php.net/manual/de/book.simplexml.php

これは、問題の解決策を見つけ、xml を解析するのに役立ちます。

于 2012-05-22T17:34:32.673 に答える
0

simplexml_load_string

DomDocument::loadXML

どちらもあなたの仕事を達成するのに役立ちます

于 2012-05-22T17:39:24.867 に答える