【问题标题】:PHP How to read XML Child Node Value in a String using PHP?PHP 如何使用 PHP 读取字符串中的 XML 子节点值?
【发布时间】:2012-05-29 05:45:12
【问题描述】:

我有一个从其他服务器动态获取其值的字符串。 字符串的值为

$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,我该如何阅读这个字符串值并提取子节点及其值?示例:

&lt;Authenticator&gt; and its value "AE1001" ?

有没有办法把它放到数组中?这样我就可以轻松打印出它的节点值?

【问题讨论】:

    标签: php xml


    【解决方案1】:

    还可以学习DOMDocumentXPath。真的值得花时间。

    $doc = new DOMDocument;
    $doc->loadXML($string1);
    
    echo $doc->getElementsByTagName('Authenticator')->item(0)->nodeValue; // AE1001
    

    【讨论】:

    • 哇!这正是我想要的,非常感谢!它简短而准确,节省了大量时间! :) 将按照您的建议阅读手册。谢谢你:)
    【解决方案2】:

    simplexml_load_string

    DomDocument::loadXML

    两者都可以帮助您完成任务

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-24
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 2012-09-09
        相关资源
        最近更新 更多