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

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

But I do not know how to create attributes in these XML (in arrays, so these attributes appear in XML). How can I build these arrays so that I can get some tags with attributes after conversion?

Thank!

+3
source share
1 answer

Looking at the source code, obviously, 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;
    }
    //...
}
+2
source

Source: https://habr.com/ru/post/1753199/


All Articles