• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3895
  • Last Modified:

simplexml_load_string returning false when loading xml?

Hello everyone -

I am encountering a problem when trying to load a simple xml stream into a simplexml_load_string object.  I am retrieving the xml from a webservice, and var_dumps at the appropriate places verify that it is being recieved properly.  However, when I instantiate the simplexml_load_string object and load the xml into it, it returns false.

I've attached the XML I receive from the var_dump just before I instantiate below.

I'm not sure why this is not loading, any help would be invaluable.
$xml_content = $web_service_result; <--- this is the result called from another function, simple xml

var_dump($xml_content); <-- this returns the xml I added below, all looks to be fine...

$xml = simplexml_load_string($xml_content); <-- I attempt to load the xml into the object here...

var_dump($xml); <-- this outputs bool(false)

Here is the XML I receive:

<?xml version="1.0" encoding="utf-8"?>
  <Address1>321 MAIN ST</Address1>
  <City>NEW YORK</City>

Open in new window

  • 2
3 Solutions
Beverley PortlockCommented:
When I do it using the XML above it works OK.

SimpleXMLElement Object
    [CustID] => 9
    [FirstName] => EMPLOYEE
    [LastName] => Smith
    [Box] => BOX
    [Member] => MIC
    [Email] => E-fulfillment2@blah.com
    [Phone] => 1236273300
    [Address1] => 321 MAIN ST
    [Address2] => SimpleXMLElement Object
            [0] =>

    [City] => NEW YORK
    [State] => NY
    [PostalCode] => 10019
    [CountryCode] => US

This makes me wonder if there is anything else in $web_service_result other than the XML so try this

$xml = simplexml_load_string( trim($xml_content) );
Beverley PortlockCommented:
I should point out that whitespace linefeeds etc BEFORE the commencement of the XML can be a problem. trim() gets rid of them.
Ray PaseurCommented:
Turn on error_reporting(E_ALL); - you may find a notice that is helpful.  Also, watch out for character entities.  Sometimes I have seen XML that contained a URL with a query string that had ampersands in it.  While they are fine in the URL, they have to be entitized to make valid XML.
DanbobAuthor Commented:
Thank you both, bportlock's solution was correct... And Ray, I greatly appreciate your assistance as well.  Thanks for taking teh time to help.  Works great now!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now