不明白 XML Entities 和 PHP SimpleXMLElement 中的输出 [英] Don't understand the output in XML Entities and PHP SimpleXMLElement

查看:21
本文介绍了不明白 XML Entities 和 PHP SimpleXMLElement 中的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 XML 中的实体,但我不明白我的结果.

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

我有一个调用外部实体的 XML 文件,这是 config.xml :

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>

这是 totalInstances.xml 文件:

Here is the file totalInstances.xml :

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

所以在 PHP 中,我在 SimpleXMLElement 类的帮助下加载文件 config.xml :

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

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

然后我用 var_dump 输出变量 $config,这是我不明白的事情:

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)

我希望有一个简单的totalInstances"节点,其中包含节点nombre".发生什么了 ?谢谢你.

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

edit :有关更多详细信息,我不明白为什么我会得到三个名为totalInstances"的对象,而文件 totalInstances.xml 中只有一个?我希望有这个输出:

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.

推荐答案

是的,这看起来确实很奇怪.但是,您不能在 SimpleXMLElement 上使用 var_dumpprint_r.这些元素有很多魔力,这里的 var_dump 是在骗你.我的意思是真的在撒谎,请参阅:

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);

给出 NULL 而根本没有 SimpleXMLElement.

Is giving NULL and no SimpleXMLElement at all.

在您的特定情况下,如果您想将文档用作具有扩展实体的 SimpleXMLElement,那么您可以使用 LIBXML_NOENT 选项(替换实体):

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);

这确实允许迭代和访问由实体表示的实体.var_dump 看起来也好多了:

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"
  }
}

这篇关于不明白 XML Entities 和 PHP SimpleXMLElement 中的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆