Zend_Config_Xml中的奇怪行为 [英] Zend_Config_Xml strange behaviour

查看:106
本文介绍了Zend_Config_Xml中的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Zend_Config_Xml中一个奇怪的问题。

I have a strange problem with Zend_Config_Xml.

下面是一个例子。

通过这个XML文件 https://gist.github.com/883465

这code:

$config = new Zend_Config_Xml('config.xml'); 
var_dump($config->get('elements')->get('element')->toArray()); 

给出了:

array(2) { 
  [0]=> 
  array(2) { 
    ["a"]=> 
    array(1) { 
      ["attr"]=> 
      string(2) "at" 
    } 
    ["e"]=> 
    array(3) { 
      [0]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
      [1]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
      [2]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
    } 
  } 
  [1]=> 
  array(2) { 
    ["a"]=> 
    array(1) { 
      ["attr"]=> 
      string(2) "at" 
    } 
    ["e"]=> 
    array(3) { 
      [0]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
      [1]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
      [2]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
    } 
  } 
} 

这个xml文件 https://gist.github.com/883469

它给了:

array(2) { 
  ["a"]=> 
  array(1) { 
    ["attr"]=> 
    string(2) "at" 
  } 
  ["e"]=> 
  array(3) { 
    [0]=> 
    array(1) { 
      ["attr"]=> 
      string(2) "at" 
    } 
    [1]=> 
    array(1) { 
      ["attr"]=> 
      string(2) "at" 
    } 
    [2]=> 
    array(1) { 
      ["attr"]=> 
      string(2) "at" 
    } 
  } 
} 

和我预期:

array(1) { 
  [0]=> 
  array(2) { 
    ["a"]=> 
    array(1) { 
      ["attr"]=> 
      string(2) "at" 
    } 
    ["e"]=> 
    array(3) { 
      [0]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
      [1]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
      [2]=> 
      array(1) { 
        ["attr"]=> 
        string(2) "at" 
      } 
    } 
  } 
} 

当你要遍历元素这是棘手

This is tricky when you want to iterate over elements

$config = new Zend_Config_Xml('config.xml'); 
foreach($config->get('elements')->get('element') as $element); 

如果有多于一个的元素是好的,但如果你只有一个,
你最终会遍历元素孩子!

which is fine if there are more then one elements, but if you have only one, you'll end up iterating over element children!

你知道吗?

编辑:

我想出了一个丑陋的解决方法:

I came up with an ugly workaround:

如果(0 ==组$ config->的get('元素') - >获取('元素')){
 //
}

if (0 !== $config->get('elements')->get('element')) { // }

这可以帮助我识别是否有下元素标记一个以上的元素。

This helps me to identify if there is more then one elements under elements tag.

非常难看。

Anithing聪明吗?

Anithing smarter?

推荐答案

似乎Zend_Config_Xml中明确坍塌等1元的集合(有一个在源的如果声明做这个)。一些可能的解决方法:

It seems that Zend_Config_Xml explicitly collapses such 1-element collections (there's an if statement in the source that does this). Some possible workarounds:


  • 超载Zend_Config_Xml中并修复装载机code,因此它不会倒塌1元集合

  • 超载Zend_Config_Xml中和过载的get(),包括在一个更清洁的方式你的丑陋的解决方法。

  • 使用SimpleXML的,而不是Zend_Config_Xml中

  • Overload Zend_Config_Xml and fix the loader code so it doesn't collapse 1-element collections
  • Overload Zend_Config_Xml and overload get() to include your ugly workaround in a cleaner way.
  • Use SimpleXML instead of Zend_Config_Xml

这篇关于Zend_Config_Xml中的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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