14

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>matt@company.co.uk</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.

hakre
  • 193,403
  • 52
  • 435
  • 836
user187580
  • 2,275
  • 11
  • 32
  • 39

4 Answers4

22

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().

ax.
  • 58,560
  • 8
  • 81
  • 72
  • 1
    Thanks any ideas how to print firstname? or any other? – user187580 Jul 02 '10 at 11:04
  • 1
    Iam expecing to print_r($data) to print as if it were an array .. 'firstname' => firstname value etc. It was working ok for me but perhaps some changes on xml data source made it this empty. – user187580 Jul 02 '10 at 11:11
  • 1
    While it might be understandable that `print_r()` on an object results in an "empty" result, that is not what is advertised in the [PHP documentation](http://www.php.net/manual/en/function.simplexml-load-string.php) for this function. In _Example #1: Interpret an XML string_ it shows the resulting object from the example XML printed just like an array would. – Quinn Comendant Mar 14 '14 at 20:01
  • I would like to extend this question for my xml data, is it possible? I applied ax. 's answer but it not work, but I have to avoid making similar question. Here is my data : http://postimg.org/image/ekln9c4vh/ and I want to get company name from it, I tried: $data->children('xs', true)->element[0]->COMPANYNAME but it falses. – user1314404 Aug 06 '14 at 03:54
  • I found a solution: $doc = DOMDocument::loadXml($xml);$desc = $doc->getElementsByTagName('COMPANYNAME')->item(0)->textContent; – user1314404 Aug 06 '14 at 04:07
  • Thank you very much, you saved my day – PЯINCƎ Oct 22 '20 at 15:56
  • `->children()` without arguments could also be helpful – sinaza Jun 29 '21 at 20:24
8

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>matt@company.co.uk</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

onkar
  • 4,427
  • 10
  • 52
  • 89
Tomasz Struczyński
  • 3,273
  • 23
  • 28
0

Or to var dump it remove the ns7 namespacing from nodes, leave them on the root:

    <?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">
    <user><id>Matt.Smith</id>
    <lastName>Smith</lastName>
    <firstName>Matt</firstName>
    <otherName></otherName>
    <gender>male</gender>
    <email>matt@company.co.uk</email>
    <locale>en</locale>
    <role><id>A</id>
    <name>System Administrator</name></role>
    <employeeNumber></employeeNumber>
    <organization>
        <id>8000</id>
        <name>Organisation Title</name>
   </organization>
    <organization>
        <id>20707</id>
        <name>London Office</name>
   </organization>
    <attribute>
        <code>0</code>
        <description>Unassigned</description>
   </attribute>
    <attribute>
        <code>0</code>
        <description>Unassigned</description>
   </attribute>
    <attribute>
        <code></code>
        <description>Unassigned</description>
   </attribute>
    <attribute>
        <code></code>
        <description>Unassigned</description></attribute>
        <attribute><code></code>
        <description>Unassigned</description>
   </attribute>
    <attribute>
        <code></code>
        <description>Unassigned</description>
       </attribute>
    <attribute>
        <code></code>
        <description>Unassigned</description>
   </attribute>
    <attribute>
        <code></code>
        <description>Unassigned</description>
   </attribute>
   </user>
</ns7:users>
Ashley
  • 5,939
  • 9
  • 39
  • 82
0

Yes I had the same issue and thought that the simplexml_load_string was returning empty since print_r ($data) or echo $data just returned empty.

but if you do $data->name then you do get a valid data ..it kinda wierd but that how it works .. so great tip.. thanks.. it worked for me

mayavi
  • 11
  • 1