devexp DEVelopment EXPerience, shared with the world!

11Apr/095

PHP DOMDocument: Convert Array to Xml

phpRecently I start working on an export module to invoice applications. One of them uses a very simple xml structure (simple nodes without attributes etc.) and therefore I wanted to create this xml also in a very simple way: from an array.

I decided the use DOM, and not e.g. SimpleXML, because I need to validate my xml with a schema.

After a quick search on the internet I found several snippets that could do the trick until I had to set the same element tag name on the same level:

<nodes>
	<node>text</node>
	<node>text</node>
</nodes>

The snippets I found use the index of an array to create the name of the element and the value for the text part. Only everyone knows that you cannot set 2 keys with the same name in an array.

So what could we do to improve this ?