Simplexml_load_string($ string)返回一个空对象,但$ string包含xml?代码如下

时间:2010-07-02 10:51:41

标签: php xml simplexml

我使用xml格式的cURL检索一些信息。

....

$xml = curl_exec($ch);

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

如果我尝试 - print_r($xml);并查看页面来源 我得到了

<?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>

这个xml全部在一行中,我手动输入了换行符以使其可读。

4 个答案:

答案 0 :(得分:13)

更新要打印名字(或任何其他),您可以使用the usual SimpleXML addressing mechanisms。你的案例有点复杂,因为你正在使用命名空间。仍然可行 - 尝试like this

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

re:我希望print_r($data)打印好像是一个数组[...] :这种期望是错误的。它肯定会很方便,但这不是它的工作原理。要打印SimpleXML对象的xml字符串表示形式,请使用asXML()

更新结束

您期望print_r($data)打印什么? SimpleXMLElement Object ( )似乎对我来说是完全有效的输出。这并不意味着xml有问题。如果您想查看SimpleXMLElement对象的实际xml,请尝试print $data->asXML()

答案 1 :(得分:5)

嗯,这不是一个空对象。实际上,如果你打印它,它会显示你向我们展示的东西。但是,如果你做的话

echo $data->asXML();

结果是正确的:

<?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/>
...

只需使用对象,因为simpleXML意味着:)

要检查它是否正确加载,请参阅doc:

  

错误/异常

     

生成E_WARNING错误消息   对于XML数据中发现的每个错误   如果错误,则抛出异常   检测

page

答案 2 :(得分:0)

或者对var转储它从节点中删除ns7命名空间,将它们留在根目录上:

    <?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>

答案 3 :(得分:0)

是的我有同样的问题,并认为simplexml_load_string返回为空,因为print_r($ data)或echo $ data刚刚返回为空。

但是如果你做了$ data-&gt; name那么你确实得到了一个有效的数据..有点奇怪,但是它是如何工作的..这么棒的小费..谢谢..它对我有用