开发者

PHP: How to convert array to XML with support to attributes (DOMi ?)

开发者 https://www.devze.com 2023-01-05 23:53 出处:网络
I\'m using DOMi ( http://domi.sourceforge.net ) to create XML from arrays. But I don\'t know how to crea开发者_开发问答te attributes in these XML (in arrays, so these attributes appear in the XML). H

I'm using DOMi ( http://domi.sourceforge.net ) to create XML from arrays.

But I don't know how to crea开发者_开发问答te attributes in these XML (in arrays, so these attributes appear in the XML). How can I construct these arrays so I can get some tags with attributes after the convertion?

Thank you!


Looking at the source code, apparently you pass the second argument "attributes" to 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;
    }
    //...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消