Reputation: 2524
I see when i started to learn ZF2 that we can use Zend\Config\Writer to write config in XML and put it into a file. I saw the PhP class DomDocument too. And i saw also SimpleXMLDocument.
I have to make a complex and extremely heavy XML with a lot of data from my Database. With DOMDocument, for 30% of the work, my code is already too complicated and not maintainable anymore.
This is a sample of how my XML has to be :
//...a lot more XML before
<Lines>
<line lineNum="Num">
</line>
<line lineNum="Num2">
</line>
</Lines>
//A lot more XML after
Those line can be created by a foreach loop. Zend\Config\Writer can't do that (especially for attribute parts) does it ?
My question is : Is exist a better way that i don't know yet for generate an XML with Zend Framework 2 ?
P.S :I'm looking for Object oriented programming solution.
Thanks for help.
Upvotes: 1
Views: 2619
Reputation: 1916
I just took a look at the Zend\Config\Writer\Xml class.
Fortunately an / the XMLWriter is already included via PHP Extension!
You can easily inspect the processConfig method() and adapt it to your needs. For my use case I even re-used the complete addBranch method().
About XMLWriter: PHP Manual
This extension represents a writer that provides a non-cached, forward-only means of generating streams or files containing XML data.
Upvotes: 0
Reputation: 1916
I'm looking for an ZF2 internal solution too, @Hooli. Hoping to get some answers on Twitter too: What is the best way to generate / write #XML in #ZF2? Make use of \Zend\Feed\Writer or an external class like #SimpleXML? Any known module?
Maybe one can use the Zend\Feed\Writer for these cases as a workaround but I would prefer a ZF2 module or similar.
Otherwise @Tim is right, SimpleXML is a good solution. For bigger files XMLWriter is recommended.
Click here for a comparison of the two and DOM.
Upvotes: 1