加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP如何使用PHP读取字符串中的XML子节点值?

发布时间:2020-12-13 22:09:46 所属栏目:PHP教程 来源:网络整理
导读:我有一个String,它从其他服务器动态获取其值. 字符串的值是 $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
我有一个String,它从其他服务器动态获取其值.
字符串的值是

$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值并提取子节点及其值?例:

<认证器>及其价值“AE1001”?

有没有办法将其放入数组?这样我就可以轻松打印出它的节点值了吗?

解决方法

还要学习 DOMDocument和 XPath.真的值得花时间.

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

echo $doc->getElementsByTagName('Authenticator')->item(0)->nodeValue; // AE1001

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读