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

时间:2010-07-05 20:29:13

标签: php arrays xml

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

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

谢谢!

1 个答案:

答案 0 :(得分:2)

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