-1

I need to generate XML structures on the fly using PHP.
I've seen many different ways and libraries, but I would like to know which is the most efficient.

In general, I have arrays and its content has to be returned as an XML structure.

Thanks in advance.

iknow
  • 8,358
  • 12
  • 41
  • 68
Cris
  • 12,124
  • 27
  • 92
  • 159
  • possible duplicate of [Multidimensional array iteration](http://stackoverflow.com/questions/2207599/multidimensional-array-iteration/2207739#2207739) - the iterator approach can easily be adjusted to use DOM or another XML lib if you need to transform this into a DOM Tree instead of just outputting the XML. – Gordon Aug 03 '10 at 09:16
  • efficient for the machine or the programmer? – bcosca Aug 03 '10 at 09:20
  • there is no general *most efficient*. It depends on what you want to do with the XML. Also see http://devzone.zend.com/article/2387 for an overview. – Gordon Aug 03 '10 at 09:25

3 Answers3

2

Most cpu/cycle efficient: probably if you build strings by hand. Most developer/time/clarity efficient: probably SimpleXML.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • That's subjective. Personally, I find SimpleXml a pain to use compared to what DOM gives me. – Gordon Aug 03 '10 at 09:28
  • Depends, when having to use namespaces, attributes or validation DOM would have my preference indeed. For just plain unchecked tag/value scenarios SimpleXML is in my opinion a lot less verbose,more to-the-point more easily readable, and I'll try some benchmarks, I suspect it's faster. It is however subjective indeed, I'm defending my opinion here, not facts :) – Wrikken Aug 03 '10 at 09:35
  • I admit I'm biased against SimpleXml. To me the Simple in SimpleXml means *lack of functionality*, not *simplicity of use*. I find the quirky namespace support, implicit interface and lack of verbosity particularly annoying. DOM just offers so much more control over the nodes - but I understand that not everyone needs/wants it :) – Gordon Aug 03 '10 at 09:44
0

You can use this Array2XML class.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 1
    Array2XML is usually much (much!) slower and more CPU intensive then SimpleXML or XMLWriter. I do not have experience with the mentioned class, but previous experiences with other code made me very wary of such design-patters. – berkes Aug 03 '10 at 09:11
  • 1
    @berkes: Agreed i have proposed this because it is easier for the noobs :) Although decision is there with OP which one he chooses. We have now suggested him both ways :) – Sarfraz Aug 03 '10 at 09:19