简体   繁体   中英

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. 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? 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 . 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://php.net/manual/de/book.simplexml.php

This would help you to find a solution for your problem and parse your xml.

simplexml_load_string

DomDocument::loadXML

both would help you to achieve your task

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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