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

use*_*580 12 php xml simplexml

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

....

$xml = curl_exec($ch);

$data = simplexml_load_string($xml);
print_r($data);
//out put - SimpleXMLElement Object ( ) 
Run Code Online (Sandbox Code Playgroud)

如果我尝试 - 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>
Run Code Online (Sandbox Code Playgroud)

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

ax.*_*ax. 17

更新:要打印firstname(或任何其他),您可以使用通常的SimpleXML寻址机制.你的案例有点复杂,因为你正在使用命名空间.仍是可行的,但-尝试一些类似这样的:

$data->children('ns7', true)->user[0]->lastName
Run Code Online (Sandbox Code Playgroud)

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

更新结束

你还期待什么print_r($data)打印?SimpleXMLElement Object ( )似乎是完全有效的输出给我.这并不意味着xml有问题.如果你想看到SimpleXMLElement对象的实际xml,试试吧print $data->asXML().


Tom*_*ski 6

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

echo $data->asXML();
Run Code Online (Sandbox Code Playgroud)

结果是正确的:

<?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/>
...
Run Code Online (Sandbox Code Playgroud)

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

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

错误/异常

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

页面