tags:

views:

35

answers:

4

I basically have to query the database to grab all the active properties, then grab content under each property for sections such as accommodations/experiences, and based on that generate basically a site map.

I'm wondering if I should go with using purely DOM methods ( would have me doing a hundred or so createElements, inside of loops, appendChild, etc ) or just do a giant string concatenation and validate that then render it as xml?

+2  A: 

I would generate it with strings, in most cases.

$myXml  = "<?xml ...... ";
$myXml .= "<rootNode>";
$myXml .= "<child>";

etc..

Fosco
Ya.. I usually do this because it's time conserving and succinct.
meder
+3  A: 

Personally, I always try to use the DOM methods for XML generation. The reason is simple, it performs all the necessary escaping and entity generation for you.

$xmlBlock = '<foo>';
$xmlBlock .= '<bar>'.htmlspecialchars('baz', ENT_NOQUOTES, 'utf-8', false).'</bar>';
$xmlBlock .= '</foo>';

Compared to:

$node = $dom->createElement('foo');
$node->appendChild($dom->createElement('bar', 'baz'));

But then again, that's just my personal preference...

ircmaxell
+1  A: 

Performance-wise, string concatenation then doing a double-pass on the string is at a disadvantage. XML validation in the case of DOM methods is done for you automatically.

stillstanding
A: 

If you're concerned about performance, use output buffering to build the XML content. Also, build or find a trivial library that maintains a tag stack, does XML escaping, etc, so that you can guarantee well-formed output. That way, you can skip the validation pass unless you really need to verify conformance to a schema or DTD.

I don't know of anything in PHP, but the Android XmlSerializer is a good model of the minimum API you need to produce guaranteed-well-formed XML without building a DOM in memory as part of the process. The code involved isn't complicated and can be built and tested separately from any applications that use it.

Walter Mundt