473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 46947
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MarjaR | last post by:
For my application I need to develop an interface with an external organisation, based on XMLHTTP messaging. Depending on the specific purpose of the communication, this external organisation...
7
by: Rathtap | last post by:
I want to write a C# application (lets call it Generator) that will receive an argument(patient account number) and dynamically generate a series of linked HTML files (claim information, payments,...
2
by: Jeff Johnson | last post by:
Hi, Does anyone know how I would code a dynamically generated CheckBoxList within a PlaceHolder? I have a function that returns an array, I then want to loop throuth the array and create...
0
by: jijo kuruvila | last post by:
How we can Dynamically generate a MS Word document using ASP.Net??? -- Jijo kuruvila trivandrum,Kerala,India
0
by: KBuser | last post by:
I'm building an internal site which will allow for extremely customizable queries to be run against our SQL Server (2000) DB. The page is done in ASP .net 2.0, with C# code behind. The initial...
1
by: KBuser | last post by:
I'm building an internal site which will allow for extremely customizable queries to be run against our SQL Server (2000) DB. The page is done in ASP .net 2.0, with C# code behind. The initial...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
1
by: Scott Zabolotzky | last post by:
I'm sure somebody has to have done this already but I can't find any good references. If I have an XML file with an associated XSD what is the best way to dynamically generate a web form with...
4
by: Mike Logan | last post by:
I would like to dynamically create a Visio diagram from .Net. I tried looking at the Visio SDK, but the samples are extremely convoluted for me to understand. Does someone have an example of...
2
by: ssmith147 | last post by:
Hi, I'm somewhat familiar with access and vb programming (I can read someone else's code, for the most part), but I'm still very green when it comes to creating solutions for my own needs. I'm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.