Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Simplexml_load_string($string)返回一个空对象,但$string包含xml?代码如下_Php_Xml_Simplexml - Fatal编程技术网

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

Php Simplexml_load_string($string)返回一个空对象,但$string包含xml?代码如下,php,xml,simplexml,Php,Xml,Simplexml,我使用xml格式的cURL检索一些信息 .... $xml = curl_exec($ch); $data = simplexml_load_string($xml); print_r($data); //out put - SimpleXMLElement Object ( ) 如果我尝试打印($xml)和查看页面源代码 我明白了 马特,史密斯 史密斯 马特 男性的 matt@company.co.uk EN A. 系统管理员 8000 组织名称 20707 伦敦办事处 0 未分配

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

....

$xml = curl_exec($ch);

$data = simplexml_load_string($xml);
print_r($data);
//out put - SimpleXMLElement Object ( ) 
如果我尝试打印($xml)和查看页面源代码 我明白了


马特,史密斯
史密斯
马特
男性的
matt@company.co.uk
EN
A.
系统管理员
8000
组织名称
20707
伦敦办事处
0
未分配
0
未分配
未分配
未分配
未分配
未分配
未分配
未分配
此xml全部在一行中,我已手动输入换行符以使其可读。

更新:要打印firstname(或任何其他名称),可以使用。您的案例稍微复杂一些,因为您使用的是名称空间。但仍然可行-尝试以下方法:

re:我期望
print\r($data)
像数组一样打印[…]:这种期望是错误的。这肯定很方便,但它不是这样工作的。要打印SimpleXML对象的xml字符串表示形式,请使用

更新结束


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

好吧,这不是一个空对象。事实上,如果你把它打印出来,它会显示你给我们看的东西。但是如果你举个例子

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

马特,史密斯
史密斯
马特
男性的
matt@company.co.uk
EN
A.
系统管理员
...
只需使用对象,因为simpleXML的目的是:)

要检查是否正确加载,请参阅文档:

错误/例外

生成E_警告错误消息 对于在XML数据中发现的每个错误 如果发生错误,则引发异常 检测到


或要从节点中删除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>

是的,我也有同样的问题,并且认为simplexml\u load\u字符串返回空,因为print\r($data)或echo$data刚刚返回空


但是如果你使用$data->name,那么你会得到一个有效的数据。这有点奇怪,但它就是这样工作的。。这么好的小费。。谢谢它对我有用

谢谢它确实有用:)我怎么打印名字,那么我怎么打印名字?谢谢你有没有打印名字的想法?或任何其他?我很想打印($data),就像打印一个数组一样firstname'=>firstname值等。它对我来说工作正常,但可能是xml数据源上的某些更改使其变为空。虽然可以理解,对象上的
print\r()
会导致“空”结果,但该函数的。在示例#1:解释一个XML字符串中,它显示从示例XML打印的结果对象,就像一个数组一样。我想为我的XML数据扩展这个问题,是否可能?我用了斧头。”’答案是否定的,但我必须避免提出类似的问题。这是我的数据:我想从中获取公司名称,我尝试了:$data->children('xs',true)->element[0]->COMPANYNAME,但它是错误的。我找到了一个解决方案:$doc=DOMDocument::loadXml($xml)$desc=$doc->getElementsByTagName('COMPANYNAME')->item(0)->textContent;因为它是一个对象,所以可以使用标准对象引用:$data->user->firstName
<?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/>
...
    <?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>