代码之家  ›  专栏  ›  技术社区  ›  Paulo Coghi

php:如何将数组转换为支持属性的XML(domi?)

  •  3
  • Paulo Coghi  · 技术社区  · 15 年前

    我在用多米尼( http://domi.sourceforge.net )从数组创建XML。

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

    谢谢您!

    1 回复  |  直到 11 年前
        1
  •  2
  •   Artefacto    15 年前

    查看源代码,显然您传递了第二个参数 "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;
        }
        //...
    }