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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3165048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:54:18  来源:igfitidea点击:

Simplexml_load_string($string) returns an empty object but $string contains xml? code below

phpxmlsimplexml

提问by user187580

I retrieve some information using cURL in xml format.

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

....

$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

如果我尝试 -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>[email protected]</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.

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

回答by ax.

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:

更新要打印名字(或任何其他),您可以使用通常的 SimpleXML 寻址机制。您的情况有点复杂,因为您使用的是名称空间。仍是可行的,但-试试这样

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

回复:我希望print_r($data)像数组一样打印 [...]:这种期望是错误的。它肯定会很方便,但它不是这样工作的。要打印 SimpleXML 对象的 xml 字符串表示形式,请使用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().

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

回答by Tomasz Struczyński

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

嗯,这不是一个空对象。事实上,如果你 print_r 它会显示你向我们展示的内容。但是,如果您例如这样做

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>[email protected]</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 :)

只需使用该对象,因为 simpleXML 旨在:)

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.

错误/异常

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

at page

页面

回答by Ashley

Or to var dump it remove the ns7 namespacing from nodes, leave them on the root:

或者 var dump 它从节点中删除 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>[email protected]</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>

回答by mayavi

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.

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

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

但是如果你做 $data->name 那么你会得到一个有效的数据..它有点奇怪但是它是如何工作的..很好的提示..谢谢..它对我有用