469,281 Members | 2,486 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

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.

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

Jul 17 '05 #1
4 46759
On 2005-07-04, pe**********@yahoo.com <pe**********@yahoo.com> wrote:
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?
Untill now i've liked the DOM(XML) extension more.. But i should make
some time to compare them against string manipulation...
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.


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>
Jul 17 '05 #2
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 -
http://www.tonymarston.co.uk/php-mysql/domxml.html
Using PHP 4's Sablotron extension to perform XSL Transformations -
http://www.tonymarston.co.uk/php-mysql/sablotron.html
Using PHP 5's DOM functions to create XML files from SQL data -
http://www.tonymarston.co.uk/php-mysql/dom.html
Using PHP 5's XSL extension to perform XSL Transformations -
http://www.tonymarston.co.uk/php-mysql/xsl.html

Hope this helps.

--
Tony Marston

http://www.tonymarston.net

<pe**********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Jul 17 '05 #3
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 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!

Thank again,
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 17 '05 #4
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 appendAnotherChild()
{
$docdoc = new DOMDocument('1.0', 'iso-8859-1');

$cd = new DomElement('cd', '');
$docdoc->appendChild($cd);
$album = new DomElement('album', 'Pure Guava');
$cd->appendChild($album);
$artist = new DomElement('artist', 'Ween');
$cd->appendChild($artist);
$year = new DomElement('year', '1992');
$cd->appendChild($year);

return $docdoc;
}

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

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

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

$docdoc=appendAnotherChild();
$xmlContent = $xml->importNode($docdoc->documentElement,true);
$xml->appendChild($xmlContent);

$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 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Rathtap | last post: by
2 posts views Thread by Jeff Johnson | last post: by
reply views Thread by jijo kuruvila | last post: by
1 post views Thread by Scott Zabolotzky | last post: by
4 posts views Thread by Mike Logan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.