PHP to dynamically generate XML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • petermichaux@yahoo.com

    PHP to dynamically generate XML

    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.



    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

  • Tim Van Wassenhove

    #2
    Re: PHP to dynamically generate XML

    On 2005-07-04, petermichaux@ya hoo.com <petermichaux@y ahoo.com> wrote:
    [color=blue]
    > 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?[/color]

    Untill now i've liked the DOM(XML) extension more.. But i should make
    some time to compare them against string manipulation...
    [color=blue]
    > 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.[/color]

    I think that in this case you're better off with a JOIN in your query
    and that resultset to your xml document.


    In the situation where you would add 2 unrelated sorts of data, your
    approach seems faisable... For example, a node with articles (main content)..
    And another node with messages (for a shoutbox as a gimmick)



    --
    Met vriendelijke groeten,
    Tim Van Wassenhove <http://timvw.madoka.be >

    Comment

    • Tony Marston

      #3
      Re: PHP to dynamically generate XML

      If you want to see a working example of code which writes database data into
      an XML file then uses XSL to transform it into HTML then take a look at
      http://www.tonymarston.net/php-mysql...plication.html. This works
      with PHP 4 and PHP 5, and includes examples of combining data from more than
      one table without a JOIN. This is based on the following articles:

      Using PHP 4's DOM XML functions to create XML files from SQL data -

      Using PHP 4's Sablotron extension to perform XSL Transformations -

      Using PHP 5's DOM functions to create XML files from SQL data -

      Using PHP 5's XSL extension to perform XSL Transformations -


      Hope this helps.

      --
      Tony Marston

      This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




      <petermichaux@y ahoo.com> wrote in message
      news:1120507283 .870837.147410@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > 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/...LT_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
      >[/color]


      Comment

      • petermichaux@yahoo.com

        #4
        Re: PHP to dynamically generate XML

        Hi Tony,

        Thanks for the info. I looked at your PHP5/DOM example for XML from SQL
        data. That is where I am headed. However 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 appendAnotherCh ild($xml), or
        make a global $xml, or create a new DomDocument inside
        appendAnotherCh ild. I cannot find any ideas like this on the web. If
        you have any ideas or tips I'd be grateful!

        Thank again,
        Peter


        <?php

        function appendAnotherCh ild()
        {
        $cd = $xml->createElement( 'cd', '');
        $collection->appendChild($c d);
        $title = $xml->createElement( 'title', 'Pure Guava');
        $cd->appendChild($t itle);
        $artist = $xml->createElement( 'artist', 'Ween');
        $cd->appendChild($a rtist);
        $year = $xml->createElement( 'year', '1992');
        $cd->appendChild($y ear);
        }

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

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

        $cd = $xml->createElement( 'cd', '');
        $collection->appendChild($c d);
        $title = $xml->createElement( 'title', 'Sailing The Seas Of Cheese');
        $cd->appendChild($t itle);
        $artist = $xml->createElement( 'artist', 'Primus');
        $cd->appendChild($a rtist);
        $year = $xml->createElement( 'year', '1991');
        $cd->appendChild($y ear);

        appendAnotherCh ild();

        $xsl = new DomDocument('1. 0', 'iso-8859-1');
        $xsl->load('collecti on.xsl');
        $proc = new XsltProcessor;
        $proc->importStyleShe et($xsl);
        $xhtml = $proc->transformToXml ($xml);
        echo $xhtml;
        ?>

        Comment

        • petermichaux@yahoo.com

          #5
          Re: PHP to dynamically generate XML

          Hi,

          After a lot of manual reading and some googling I've found a way to do
          this. I think this will serve my purpose but it is not too flexible. If
          you can see any ways to improve this method or a completely different
          way please suggest it.

          Also I've used two ways to create new elements:

          $cd = new DomElement('cd' , '');

          $cd = $xml->createElement( 'cd', '');

          I like the first way better because I do not have to use the
          domdocument instance name. Is there any reason to use the second
          instead of the first?

          Thanks,
          Peter

          <?php

          function appendAnotherCh ild()
          {
          $docdoc = new DOMDocument('1. 0', 'iso-8859-1');

          $cd = new DomElement('cd' , '');
          $docdoc->appendChild($c d);
          $album = new DomElement('alb um', 'Pure Guava');
          $cd->appendChild($a lbum);
          $artist = new DomElement('art ist', 'Ween');
          $cd->appendChild($a rtist);
          $year = new DomElement('yea r', '1992');
          $cd->appendChild($y ear);

          return $docdoc;
          }

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

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

          $cd = $xml->createElement( 'cd', '');
          $collection->appendChild($c d);
          $album = $xml->createElement( 'album', 'Sailing The Seas Of Cheese');
          $cd->appendChild($a lbum);
          $artist = $xml->createElement( 'artist', 'Primus');
          $cd->appendChild($a rtist);
          $year = $xml->createElement( 'year', '1991');
          $cd->appendChild($y ear);

          $docdoc=appendA notherChild();
          $xmlContent = $xml->importNode($do cdoc->documentElemen t,true);
          $xml->appendChild($x mlContent);

          $xsl = new DomDocument('1. 0', 'iso-8859-1');
          $xsl->load('collecti on.xsl');
          $proc = new XsltProcessor;
          $proc->importStyleShe et($xsl);
          $xhtml = $proc->transformToXml ($xml);
          echo $xhtml;
          ?>

          Comment

          Working...