php 为什么 SimpleXMLElement 返回一个空对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6651395/
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-26 00:54:41  来源:igfitidea点击:

Why does SimpleXMLElement returns a empty object?

phpxmlsimplexml

提问by Italo André

I am using the SimpleXMLElement method to load a string, but it just does not work. My code is:

我正在使用 SimpleXMLElement 方法加载字符串,但它不起作用。我的代码是:

$xml = new SimpleXMLElement($content);
var_dump($xml);

And the var_dump returns

并且 var_dump 返回

object(SimpleXMLElement)#104 (0) { }

The var $content is correctedly setted and filled, if I echo this var, this is the result:

var $content 已正确设置和填充,如果我回应此 var,结果如下:

<?xml version="1.0" encoding="utf-8" ?> 
<string xmlns="http://www.jadlog.com.br/JadlogWebService/services"> 
    <Jadlog_Tracking_Consultar> 
        <ND> 
            <Numero>10080780714284</Numero> 
            <Status>ENTREGUE</Status> 
            <DataHoraEntrega>16/06/2011</DataHoraEntrega> 
            <Recebedor>DIEGO OLIVEIRA CRUZ</Recebedor> 
            <Documento>0883473380</Documento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>10/06/2011 19:51</DataHoraEvento> 
                <Descricao>EMISSAO </Descricao> 
                <Observacao>CO SAO PAULO 08</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>10/06/2011 20:12</DataHoraEvento> 
                <Descricao>TRANSFERENCIA </Descricao> 
                <Observacao>TECA JAD SAO</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>10/06/2011 20:53</DataHoraEvento> 
                <Descricao>ENTRADA </Descricao> 
                <Observacao>TECA JAD SAO</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>10/06/2011 21:05</DataHoraEvento> 
                <Descricao>TRANSFERENCIA </Descricao> 
                <Observacao>FL JAD SALVADOR</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>12/06/2011 10:27</DataHoraEvento> 
                <Descricao>ENTRADA </Descricao> 
                <Observacao>FL JAD SALVADOR</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>12/06/2011 11:21</DataHoraEvento> 
                <Descricao>TRANSFERENCIA </Descricao> 
                <Observacao>CO SALVADOR 02</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>12/06/2011 11:25</DataHoraEvento> 
                <Descricao> ATRASO TRANSPORTE</Descricao> 
                <Observacao>FL JAD SALVADOR</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>12/06/2011 11:51</DataHoraEvento> 
                <Descricao>TRANSFERENCIA </Descricao> 
                <Observacao>CO SALVADOR 02</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>14/06/2011 14:28</DataHoraEvento> 
                <Descricao>ENTRADA </Descricao> 
                <Observacao>CO SALVADOR 02</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>14/06/2011 16:14</DataHoraEvento> 
                <Descricao>ENTRADA </Descricao> 
                <Observacao>CO SALVADOR 02</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>14/06/2011 18:10</DataHoraEvento> 
                <Descricao>EM ROTA </Descricao> 
                <Observacao>CO SALVADOR 02</Observacao> 
            </Evento> 
            <Evento> 
                <Codigo></Codigo> 
                <DataHoraEvento>16/06/2011 08:59</DataHoraEvento> 
                <Descricao>ENTREGUE </Descricao> 
                <Observacao>CO SALVADOR 02</Observacao> 
            </Evento> 
        </ND> 
    </Jadlog_Tracking_Consultar> 
</string>

Could someone help me out with this?

有人可以帮我解决这个问题吗?

采纳答案by Italo André

The problem was with the webservice, it was returning a Soap Object, but I was able to see it just in the source-code. As the XML was showing ok, I wasn't thinking there was a problem with the request. After checking the source-code and using Soap to handle the requests, I could figure it out. SimpleXML works nice.

问题出在网络服务上,它返回一个 Soap 对象,但我只能在源代码中看到它。由于 XML 显示正常,我认为请求没有问题。检查源代码并使用 Soap 处理请求后,我可以弄清楚。SimpleXML 工作得很好。

Thanks to everyone for the help.

感谢大家的帮助。

回答by Ross

It may not be 'empty' - try $xml->children();for example. As far as I know SimpleXmlElement doesn't behave like other PHP objects, in that when you var_dumpit (although I think it works with print_rfor some reason) it doesn't show you its members.

它可能不是“空” -$xml->children();例如尝试。据我所知,SimpleXmlElement 的行为不像其他 PHP 对象,因为当您使用var_dump它时(尽管我认为它print_r出于某种原因可以使用),它不会向您显示其成员。

回答by FinalForm

It's working properly. The XML has just been stored in an object. So you can access it like any other object. So when you're dumping the contents of $xml, it's correct that you see it saying object. See below example. The below is just some random example. I just wanted to show you the arrow ->.

它工作正常。XML 刚刚存储在一个对象中。因此,您可以像访问任何其他对象一样访问它。因此,当您转储 $xml 的内容时,您看到它说对象是正确的。见下面的例子。以下只是一些随机示例。我只是想向您展示箭头 ->。

<?php    
$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie[0]->plot;
?>