Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplexml_load_string($string) returns an empty object but $string contains xml? code below

Tags:

php

xml

simplexml

I retrieve some information using cURL in xml format.

....

$xml = curl_exec($ch);

$data = simplexml_load_string($xml);
print_r($data);
//out put - SimpleXMLElement Object ( ) 

if I try - print_r($xml); and view page source I get

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ns7:users xmlns="http://www.example.com/xml/ns/rs" 
        xmlns:ns2="http://www.example.com/xml/ns/users" 
        xmlns:ns3="http://www.example.com/2004/11/tHistory" 
        xmlns:ns4="http://www.example.com/fsi/tHistory" 
        xmlns:ns5="http://www.example.com/2005/10/tHistory" 
        xmlns:ns6="http://www.example.com/2010/03/cs" 
        xmlns:ns7="http://www.example.com/2005/10/users" 
        xmlns:ns8="http://www.example.com/2010/03/tHistory">
    <ns7:user><ns7:id>Matt.Smith</ns7:id>
    <ns7:lastName>Smith</ns7:lastName>
    <ns7:firstName>Matt</ns7:firstName>
    <ns7:otherName></ns7:otherName>
    <ns7:gender>male</ns7:gender>
    <ns7:email>[email protected]</ns7:email>
    <ns7:locale>en</ns7:locale>
    <ns7:role><ns7:id>A</ns7:id>
    <ns7:name>System Administrator</ns7:name></ns7:role>
    <ns7:employeeNumber></ns7:employeeNumber>
    <ns7:organization>
        <ns7:id>8000</ns7:id>
        <ns7:name>Organisation Title</ns7:name>
    </ns7:organization>
    <ns7:organization>
        <ns7:id>20707</ns7:id>
        <ns7:name>London Office</ns7:name>
    </ns7:organization>
    <ns7:attribute>
        <ns7:code>0</ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code>0</ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description></ns7:attribute>
        <ns7:attribute><ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
        </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    <ns7:attribute>
        <ns7:code></ns7:code>
        <ns7:description>Unassigned</ns7:description>
    </ns7:attribute>
    </ns7:user>
</ns7:users>

this xml is all in one line and I have manually entered line breaks to make it readable.

like image 815
user187580 Avatar asked Jul 02 '10 10:07

user187580


People also ask

What does simplexml_ load_ string return?

Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is simplexml_ load_ string in php?

The simplexml_load_string() function converts a well-formed XML string into an object.


2 Answers

UPDATE: to print firstname (or any other), you can use the usual SimpleXML addressing mechanisms. your case is a little more complicated because you are using namespaces. still workable though - try something like this:

$data->children('ns7', true)->user[0]->lastName

re: i am expecting print_r($data) to print as if it were an array [...]: this expectation is wrong. it would surely be handy, but that's not how it works. to print a SimpleXML object's xml string representation, use asXML().

UPDATE END

what are you expecting print_r($data) to print? SimpleXMLElement Object ( ) seems to be perfectly valid output to me. it doesn't mean that there is something wrong with the xml. if you want to see the actual xml of your SimpleXMLElement Object, try print $data->asXML().

like image 146
ax. Avatar answered Oct 22 '22 19:10

ax.


Well, this is not an empty object. Indeed, if you print_r it it shows what you showed us. But if you for example do

echo $data->asXML();

the result will be correct:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns7:users xmlns="http://www.example.com/xml/ns/rs" xmlns:ns2="http://www.example.com/xml/ns/users" xmlns:ns3="http://www.example.com/2004/11/tHistory" xmlns:ns4="http://www.example.com/fsi/tHistory" xmlns:ns5="http://www.example.com/2005/10/tHistory" xmlns:ns6="http://www.example.com/2010/03/cs" xmlns:ns7="http://www.example.com/2005/10/users" xmlns:ns8="http://www.example.com/2010/03/tHistory">
    <ns7:user><ns7:id>Matt.Smith</ns7:id>
    <ns7:lastName>Smith</ns7:lastName>
    <ns7:firstName>Matt</ns7:firstName>
    <ns7:otherName/>
    <ns7:gender>male</ns7:gender>
    <ns7:email>[email protected]</ns7:email>
    <ns7:locale>en</ns7:locale>
    <ns7:role><ns7:id>A</ns7:id>
    <ns7:name>System Administrator</ns7:name></ns7:role>
    <ns7:employeeNumber/>
...

Just use the object as simpleXML is meant to :)

To check if it loaded correctly, see the doc:

Errors/Exceptions

Produces an E_WARNING error message for each error found in the XML data and throws an exception if errors were detected.

at page

like image 44
Tomasz Struczyński Avatar answered Oct 22 '22 19:10

Tomasz Struczyński