0

I use entities in XML and I don't understand my results.

I have an XML file wich calls an external entity, this is config.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE config [
    <!ENTITY totalInstances SYSTEM "totalInstances.xml">
]>
<config>
    &totalInstances;
</config>

Here is the file totalInstances.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<totalInstances>
    <nombre>45</nombre>
</totalInstances>

So in PHP I load the file config.xml with the help of the Class SimpleXMLElement :

$config = simplexml_load_file('config.xml');

Then I output the variable $config with a var_dump, and here is the thing I don't understand :

object(SimpleXMLElement)[3]
  public 'totalInstances' => 
    object(SimpleXMLElement)[5]
      public 'totalInstances' => 
        object(SimpleXMLElement)[6]
          public 'totalInstances' => 
            object(SimpleXMLElement)[8]
              public 'nombre' => string '45' (length=2)

I expected to have a simple "totalInstances" node which contains the node "nombre" . What happens ? Thanks you.

edit : For more details, I don't understand why I get three objects named "totalInstances" while there are only one in the file totalInstances.xml ? I expected to have this output :

object(SimpleXMLElement)[3]
      public 'totalInstances' => 
            object(SimpleXMLElement)[8]
                public 'nombre' => string '45' (length=2)

Also, I'm not sure to understand what means the number between the "[]" in the output.

hakre
  • 193,403
  • 52
  • 435
  • 836
Antoine-Ka
  • 29
  • 5
  • What is it you don't understand? I can only see one `nombre` node there which should be correct? – silkfire Apr 20 '13 at 11:11
  • @silkfire, I think the OP doesn't understand why there are 3 `totalInstances` (SimpleXML)elements, given he only defined one in his XML document... – Havelock Apr 20 '13 at 11:15

1 Answers1

1

Yes, this does really look weird. However, you can not use var_dump or print_r on a SimpleXMLElement. These elements are with a lot of magic and the var_dump here is lying to you. I mean really lying, see:

var_dump($config->totalInstances->totalInstances);

Is giving NULL and no SimpleXMLElement at all.

In your specific case if you want to make use of the document as a SimpleXMLElement with expanded entities, then you can use the LIBXML_NOENT option (substitute entities):

$config = simplexml_load_file('config.xml', NULL, LIBXML_NOENT);

This does allow to iterate over and access the entities that are represented by the entity/ies. The var_dump then looks much better, too:

class SimpleXMLElement#4 (1) {
  public $totalInstances =>
  class SimpleXMLElement#3 (1) {
    public $nombre =>
    string(2) "45"
  }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • It might not be crystal clear from the answer that `var_dump`/`print_r` are not that well for debugging simplexml objects in general, not only specific in your case. However the case you've found is exceptional as well. Compare with [How to get an attribute with SimpleXML?](http://stackoverflow.com/q/3410520/367456) which shows another case where this is "lying". – hakre Apr 22 '13 at 17:28
  • I guess that the magic and the lies means "something more complicated than you expect". In few words, where do this magic come from ? Thanks a lot. – Antoine-Ka Apr 22 '13 at 17:49
  • That is because the SimpleXMLElement is an internal object (built into a PHP extension). So it can do whatever it wants, even creating its own `var_dump`/`print_r` output. If you want to look inside such an element, I often find `echo $element->asXML()` often helpful, it gives the XML of that element. – hakre Apr 22 '13 at 17:55