simplexml_load_string returning false when loading xml?

Posted on 2010-01-08
Last Modified: 2012-05-08
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

Question by:Danbob
    LVL 34

    Accepted Solution

    When I do it using the XML above it works OK.

    SimpleXMLElement Object
        [CustID] => 9
        [FirstName] => EMPLOYEE
        [LastName] => Smith
        [Box] => BOX
        [Member] => MIC
        [Email] =>
        [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) );
    LVL 34

    Assisted Solution

    by:Beverley Portlock
    I should point out that whitespace linefeeds etc BEFORE the commencement of the XML can be a problem. trim() gets rid of them.
    LVL 107

    Assisted Solution

    by:Ray Paseur
    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.

    Author Comment

    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!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now