Simplexml_load_string($string) 返回一个空对象但 $string 包含 xml?下面的代码 [英] Simplexml_load_string($string) returns an empty object but $string contains xml? code below

查看:23
本文介绍了Simplexml_load_string($string) 返回一个空对象但 $string 包含 xml?下面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<代码>....$xml = curl_exec($ch);$data = simplexml_load_string($xml);打印_r($数据);//输出 - SimpleXMLElement 对象 ( )

如果我尝试 - print_r($xml); 并查看页面源代码我明白了

<ns7:user><ns7:id>Matt.Smith</ns7:id><ns7:lastName>史密斯</ns7:lastName><ns7:firstName>马特</ns7:firstName><ns7:otherName></ns7:otherName><ns7:gender>男性</ns7:gender><ns7:email>matt@company.co.uk</ns7:email><ns7:locale>en</ns7:locale><ns7:role><ns7:id>A</ns7:id><ns7:name>系统管理员</ns7:name></ns7:role><ns7:employeeNumber></ns7:employeeNumber><ns7:组织><ns7:id>8000</ns7:id><ns7:name>组织名称</ns7:name></ns7:organization><ns7:组织><ns7:id>20707</ns7:id><ns7:name>伦敦办公室</ns7:name></ns7:organization><ns7:属性><ns7:code>0</ns7:code><ns7:description>未分配</ns7:description></ns7:attribute><ns7:属性><ns7:code>0</ns7:code><ns7:description>未分配</ns7:description></ns7:attribute><ns7:属性><ns7:code></ns7:code><ns7:description>未分配</ns7:description></ns7:attribute><ns7:属性><ns7:code></ns7:code><ns7:description>未分配</ns7:description></ns7:attribute><ns7:attribute><ns7:code></ns7:code><ns7:description>未分配</ns7:description></ns7:attribute><ns7:属性><ns7:code></ns7:code><ns7:description>未分配</ns7:description></ns7:attribute><ns7:属性><ns7:code></ns7:code><ns7:description>未分配</ns7:description></ns7:attribute><ns7:属性><ns7:code></ns7:code><ns7:description>未分配</ns7:description></ns7:attribute></ns7:user></ns7:users>

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

解决方案

UPDATE:打印名字(或任何其他),你可以使用通常的 SimpleXML 寻址机制.您的情况有点复杂,因为您使用的是名称空间.仍然可行 - 尝试一些像这样:

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

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.

解决方案

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

这篇关于Simplexml_load_string($string) 返回一个空对象但 $string 包含 xml?下面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆