PHP to dynamically generate XML

Discussion in 'XML' started by petermichaux@yahoo.com, Jul 4, 2005.

  1. Guest

    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
    , Jul 4, 2005
    #1
    1. Advertising

  2. Andy Dingley Guest

    On 4 Jul 2005 12:10:31 -0700, wrote:

    >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".
    Andy Dingley, Jul 5, 2005
    #2
    1. Advertising

  3. Guest

    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
    , Jul 5, 2005
    #3
  4. wrote:
    > 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)

    >
    > 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
    >
    Tjerk Wolterink, Jul 5, 2005
    #4
  5. Andy Dingley Guest

    On 4 Jul 2005 17:30:01 -0700, wrote:

    >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.
    Andy Dingley, Jul 5, 2005
    #5
  6. Guest

    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;
    ?>
    , Jul 5, 2005
    #6
  7. Guest

    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
    , Jul 6, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    459
  2. MarjaR
    Replies:
    5
    Views:
    5,041
    MarjaR
    Oct 6, 2005
  3. Scott Zabolotzky

    Dynamically generate form from XML & XSD?

    Scott Zabolotzky, Aug 29, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,383
    =?Utf-8?B?Q2hhbmR1?=
    Aug 30, 2006
  4. Rajive Narain
    Replies:
    0
    Views:
    1,471
    Rajive Narain
    Sep 18, 2009
  5. Matt
    Replies:
    3
    Views:
    321
    Bob Barrows [MVP]
    Apr 23, 2004
Loading...

Share This Page