PHP to dynamically generate XML

P

petermichaux

Hi,

I am interesting in using PHP to dynamically create an XML document and
then use XSLT to transform to XHTML. I have come across two methods for
doing this. I made two equivalent examples so you can see what I mean.

http://members.shaw.ca/petermichaux/store/XML_XSLT_method.html

I am interested in critisim of how I implemented each method. However
my main problem is deciding between methods. Which method is better for
bigger programs where the bits of the XML data are assembled by various
php functions that I write?

For example, one php function queries a database table for a list of CD
titles. A second php function loops through this CD title list and gets
the track listing for each CD from another table in the database. Then
the composite XML data (ie. CD title and tracks as nodes) can be
tranformed to XHTML.

I hope this is clear. I think it is probably pretty standard and I'm
sure many people do exactly what I want to do.

Thanks,
Peter
 
A

Andy Dingley

I am interested in critisim of how I implemented each method.

In the second (correct) one you use a DOM and append various elements to
it.

In the first one you generate XML as a string (bad) and then get the DOM
to parse it (super bad). This is ugly, non-robust and slow. Don't
duplicate in your application code things that the DOM should be doing.

For example, one php function queries a database table for a list of CD
titles. A second php function loops through this CD title list and gets
the track listing for each CD from another table in the database.

That's very bad database querying technique. Rather than one query with
a join in it, it's separate queries for the two steps - and the second
step is repeated. If you want long discussions of why this is just so
bad performance-wise, search for "round tripping".
 
P

petermichaux

Hi Andy,

Thanks for the good feedback!

I was guessing that strings must be bad because if the DOM functions
exist there must be a reason. The simplicity of the strings was
appealing and the DOMDocument->loadXML() documentation lead me in the
string direction.

I should have picked a different example for the database technique. I
wouldn't actually do that example that way. I understand what you are
saying about round tripping. Thanks for mentioning it though.

I need to think up a better example. Perhaps I'll be back later:)

-Peter
 
T

Tjerk Wolterink

Hi Andy,

Thanks for the good feedback!

I was guessing that strings must be bad because if the DOM functions
exist there must be a reason. The simplicity of the strings was
appealing and the DOMDocument->loadXML() documentation lead me in the
string direction.

Strings are not bad,
it improves your code readability,
ok but the performance is not that good.
But what if performance is not very important.

Note, you forgot one other method.

I have xsl and xml files with php code in it between processing
instructions:

<?php

?>

Now i load the documents in a DOM (zml and xslt),
then i do the transformation and in the end i do
a eval("?>".$tranform_output_xml."<?"); to run the php code
inside the generated xnl,
this is very good for code generating.

If you can use java use the xslt compiler, it
compiles xslt documents to bytecode files wich
are fast. (google on xalan)
 
A

Andy Dingley

The simplicity of the strings was
appealing and the DOMDocument->loadXML() documentation lead me in the
string direction.

This is an example of the "golden hammer" anti-pattern.

_Your_ code is simple. But what's happening inside? You're still making
the server perform a load and parse operation, which is complex and
slow. If you plan to think about efficiency in the future, you need to
be thinking about what's under the hood, whether you wrote that piece of
the code or not.

In this trivial example, the diference is trivial. But this is a very
real risk in XML work. I once worked on a piece of code that "counted to
3" (allocating new identifiers) by retrieving 20MB of complex XML from a
database, then using a simple XPath statement to find an element and
count its children, before throwing away all the other data. It ran like
a _slug_!

Your "write a string, then parse it" approach could be right if it's a
small example and very prone to change. However I'd always be wary of
it, it wouldn't be my first choice for how to code it, and although I
might even use it, I'd not recommend it in a "which technique is best?"
question.
 
P

petermichaux

I would like to be able to use
various php functions (potentially in different php files) to append
various nodes to the DomDocument. Below is a simple example but a rough
idea of what I want to do. The example does not work. I do not know if
I should pass arguments to the function appendAnotherChild($xml), or
make a global $xml, or create a new DomDocument inside
appendAnotherChild. I cannot find any ideas like this on the web. If
you have any ideas or tips I'd be grateful!

Thanks,
Peter

<?php

function appendAnotherChild()
{
$cd = $xml->createElement('cd', '');
$collection->appendChild($cd);
$title = $xml->createElement('title', 'Pure Guava');
$cd->appendChild($title);
$artist = $xml->createElement('artist', 'Ween');
$cd->appendChild($artist);
$year = $xml->createElement('year', '1992');
$cd->appendChild($year);
}

$xml = new DomDocument('1.0', 'iso-8859-1');

$collection = $xml->createElement('collection', '');
$xml->appendChild($collection);

$cd = $xml->createElement('cd', '');
$collection->appendChild($cd);
$title = $xml->createElement('title', 'Sailing The Seas Of Cheese');
$cd->appendChild($title);
$artist = $xml->createElement('artist', 'Primus');
$cd->appendChild($artist);
$year = $xml->createElement('year', '1991');
$cd->appendChild($year);

appendAnotherChild();

$xsl = new DomDocument('1.0', 'iso-8859-1');
$xsl->load('collection.xsl');
$proc = new XsltProcessor;
$proc->importStyleSheet($xsl);
$xhtml = $proc->transformToXml($xml);
echo $xhtml;
?>
 
P

petermichaux

Hi,

I found a way to do this but I don't know if it is a great way. I
posted it on the comp.lang.php newsgroup. If you have any more
suggestions I'd like to hear them but I don't want to double post too
much:)

Thanks,
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top