0

I must be missing something simple here and it's driving me crazy.

I'm making a call to a web services api and getting back some xml:

<?xml version="1.0" encoding="utf-16"?>
<MTSMember xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LastName>Smith</LastName>
  <FirstName>john</FirstName>
  <MemberNo>xxxxxxxx</MemberNo>
  <Club>None</Club>
  <JoinDate>2013-05-14</JoinDate> 
  <Email>[email protected]</Email>
</MTSMember>

I then need to process this xml to get the email address. but I'm just getting an empty result using the code below:

$xml_result = simplexml_load_string($xml_string_above);
echo $xml_result->MTSMember[0]->Email;

Can someone point me in the right direction. I've read through several other answers trying out various solutions, but can't seem to get it to work.

Edit: This was the last tutorial i tried out http://blog.teamtreehouse.com/how-to-parse-xml-with-php5

3 Answers 3

2

That should be:

echo $xml_result->Email;

Because simplexml_load_string() is loading MTSMember as main SimpleXMLElement.

Codepad Example

Sign up to request clarification or add additional context in comments.

3 Comments

Hmmm. Seems to run fine on there, but still doesn't run on my local machine. I'm tailing the php error log, and it's not throwing any errors, it's just not giving me anything in the $xml_result.
Make sure the xml is loaded.
the simplexml_load_string is returning 0; I'm walking through the run with xdebug. Do you mean loaded into the string, it is?
0

Try this

$xml=new SimpleXMLElement($str);
$result=$xml->xpath('//Email');
foreach ($result as $Email)
  echo $Email . "<br>";

simply

$xml=new SimpleXMLElement($str);
echo $xml[0]->Email;

update:

The below just for your comment , the whole what i tried

$str='<?xml version="1.0" encoding="utf-8"?>
<MTSMember xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LastName>Smith</LastName>
  <FirstName>john</FirstName>
  <MemberNo>xxxxxxxx</MemberNo>
  <Club>None</Club>
  <JoinDate>2013-05-14</JoinDate> 
  <Email>[email protected]</Email>
</MTSMember>';

$xml=new SimpleXMLElement($str);
echo $xml[0]->Email;
 //OR
$xml=new SimpleXMLElement($str);
$result=$xml->xpath('//Email');
foreach ($result as $Email)
  echo $Email . "<br>";

Be happy :)

4 Comments

When I pass the xml string to it, I get an error saying that 'String could not be parsed as XML'. Looks like xml to me, I'm at a loss for what is going on here.
Your update doesn't throw an error but nothing echo's for me. I run an echo 'xx'; before your script and an echo 'yy'; after and all i get is xxyy.
The update worked that time when I copied it. I'm going to try to run the xml from the api through. Thanks for you help!
your comment got me moving in the right direction. I had to do a search and replace on the xml to change the utf-16 to utf 8, and then I did a uft-8 function on it to be sure: $str = str_replace('encoding="utf-16"', 'encoding="utf-8"', $str); $xml=new SimpleXMLElement(utf8_encode($str));
0

This should work fine

$xml = new SimpleXMLElement($xml_string_above);

$dc = $xml->email;
echo $dc;

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.