[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


simplexml_load_string returning false when loading xml?

Posted on 2010-01-08
Medium Priority
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
  • 2
LVL 34

Accepted Solution

Beverley Portlock earned 332 total points
ID: 26211633
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) );
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 332 total points
ID: 26211638
I should point out that whitespace linefeeds etc BEFORE the commencement of the XML can be a problem. trim() gets rid of them.
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 168 total points
ID: 26211877
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

ID: 26211916
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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