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 回答