【问题标题】:PHP: How to convert array to XML with support to attributes (DOMi ?)PHP:如何在支持属性的情况下将数组转换为 XML(DOMi?)
【发布时间】:2011-03-12 01:15:52
【问题描述】:

我正在使用 DOMi (http://domi.sourceforge.net) 从数组创建 XML。

但我不知道如何在这些 XML 中创建属性(在数组中,因此这些属性出现在 XML 中)。如何构造这些数组,以便在转换后获得一些带有属性的标签?

谢谢!

【问题讨论】:

  • 如果你知道另一种方法,不用DOMi,你也可以回答!

标签: php arrays xml


【解决方案1】:

查看源代码,显然您将第二个参数"attributes" 传递给attachToXml

public function attachToXml($data, $prefix, &$parentNode = false) {
    if(!$parentNode) {
        $parentNode = &$this->mainNode;
    }
    // i don't like how this is done, but i can't see an easy alternative
    // that is clean. if the prefix is attributes, instead of creating
    // a node, just put all of the data onto the parent node as attributes
    if(strtolower($prefix) == 'attributes') {
        // set all of the attributes onto the node
        foreach($data as $key=>$val)
            $parentNode->setAttribute($key, $val);

        $node = &$parentNode;
    }
    //...
}

【讨论】:

  • 哇!这正是我想要的!
猜你喜欢
  • 2013-11-24
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 2014-03-29
相关资源
最近更新 更多