Simplexml_load_string($string) returns an empty ob

2020-03-10 05:09发布

问题:

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.

回答1:

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



回答2:

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



回答3:

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>


回答4:

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