4

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?

3 Answers 3

12

Also learn DOMDocument and XPath. It really worth the time.

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

echo $doc->getElementsByTagName('Authenticator')->item(0)->nodeValue; // AE1001
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! This is exactly what i was looking for, thanks a lot man! its short and precise, saves a lot of time! :) Will go through the manuals as you have suggested. Thank you :)
1

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.

Comments

0

simplexml_load_string

DomDocument::loadXML

both would help you to achieve your task

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.