Don't understand the output in XML Entities and PHP SimpleXMLElement

后端 未结 1 344
执念已碎
执念已碎 2020-12-22 10:12

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 :



        
相关标签:
1条回答
  • 2020-12-22 10:32

    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"
      }
    }
    
    0 讨论(0)
提交回复
热议问题