5

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

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

And the var_dump returns

object(SimpleXMLElement)#104 (0) { }

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

<?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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Italo André
  • 472
  • 2
  • 7
  • 12
  • It's well-formed, right? – BoltClock Jul 11 '11 at 14:14
  • 2
    i don't know if its a bug (or a feature), but try casting it to string. e.g. var_dump((string)$xml); also, if you're wishing it will dump the whole tree, you'd better stop wishing. – yoavmatchulsky Jul 11 '11 at 14:29
  • @BoltClock I believe it is. I'm not sure, though, as I am new to XML. And also, this XML was not created by me, I get it dynamically from a webservice. – Italo André Jul 11 '11 at 14:52
  • I followed the XML example from here http://www.php.net/manual/en/simplexml.examples-basic.php and mentioned @FinalForm and it worked. So I'm doing something wrong. I try to echo $xml->Evento[0]->DataHoraEvento and I got nothing. The var_dump returns NULL. – Italo André Jul 11 '11 at 15:01
  • 1
    @Italo: If you found the solution yourself, please post it as an answer and accept. Don't edit an answer into a question. – Lightness Races in Orbit Jul 11 '11 at 16:55
  • Thanks to the heads up, @Tomalak. But I'm conscient about it, I just didn't have reputation enough to add a answer to a question made by myself. Now I can enter the answer (as I just did), but I need to wait some hours to accept it. – Italo André Jul 12 '11 at 18:35
  • @Italo: That's fine. Wait, then accept. Thanks for contributing! – Lightness Races in Orbit Jul 12 '11 at 23:44

3 Answers3

3

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_dump it (although I think it works with print_r for some reason) it doesn't show you its members.

Ross
  • 46,186
  • 39
  • 120
  • 173
  • 2
    Both `var_dump` and `print_r` will attempt to give you some data, but due to the "magic" API hidden behind SimpleXML objects, they don't always show you everything that's there. – IMSoP Oct 14 '13 at 17:24
2

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.

Thanks to everyone for the help.

Italo André
  • 472
  • 2
  • 7
  • 12
1

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

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

echo $xml->movie[0]->plot;
?>
Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39
  • I followed this example and it was working. (I got the XML from the PHP Manual page) So it means that there is some error with the XML I'm using or in the way I'm calling the elements. I tried echo $xml->Evento[0]->DataHoraEvento and I got nothing. – Italo André Jul 11 '11 at 14:54
  • 1
    @Italo André: You probably got nothing because your document has a default namespace and with namespaces SimpleXML causes hair loss. – jasso Jul 11 '11 at 18:47