简体   繁体   English

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

[英]PHP How to read XML Child Node Value in a String using PHP?

I have a String which gets its value dynamically from other server. 我有一个String,它从其他服务器动态获取其值。 Value of string is 字符串的值是

$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>';

My Question is, generally we use *$xml = simplexml_load_file("test1.xml");* to load XML files, but here in my requirement it is a String , how can i read this String value and extract the child node and its value? 我的问题是,通常我们使用* $ xml = simplexml_load_file(“test1.xml”); *来加载XML文件,但在我的要求中它是一个String ,我怎样才能读取这个String值并提取子节点及其值? Example: 例:

<Authenticator> and its value "AE1001" ?

Is there a way to put this into Array? 有没有办法将其放入数组? so that its easy for me to print out its node value? 这样我就可以轻松打印出它的节点值了吗?

Also learn DOMDocument and XPath . 还学习DOMDocumentXPath It really worth the time. 真的值得花时间。

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

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

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

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

This would help you to find a solution for your problem and parse your xml. 这将帮助您找到问题的解决方案并解析您的xml。

simplexml_load_string simplexml_load_string

DomDocument::loadXML 的DomDocument :: loadXML的

both would help you to achieve your task 两者都可以帮助你完成任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM