473,804 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 46966
On 2005-07-04, pe**********@ya hoo.com <pe**********@y ahoo.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**********@y ahoo.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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 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;
?>

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 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;
?>

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2022
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 will send me various XML schemas (XSD documents) specifying the structure for the message interfaces. I need to be able to interpret these XSD documents and dynamically generate my database structure based on that. But... I don't have a clue...
7
25373
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, denials etc) by querying a database based on the argument. This Generator can be called from an aspx web page, a Cold Fusion page as well as from a windows application. 1. Does this C# app have to be a console application to receive arguments?...
2
2798
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 CheckBoxes with values returned from the array. So far all I have are CheckBoxes without values across my screen. Also, how would I place a "<br />" between all these CheckBoxes so that they
0
357
by: jijo kuruvila | last post by:
How we can Dynamically generate a MS Word document using ASP.Net??? -- Jijo kuruvila trivandrum,Kerala,India
0
1490
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 page has a checkboxlist which is generated from a query which returns all the user tables in our database. This is checkboxlistTables. For each table selected in this list I want to dynamically generate a new checkboxlist in a (HTML) table on the...
1
2840
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 page has a checkboxlist which is generated from a query which returns all the user tables in our database. This is checkboxlistTables. For each table selected in this list I want to dynamically generate a new checkboxlist in a (HTML) table on the...
9
1868
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, extra_information): super(MetaThing, cls).__init__(name, bases, dict)
1
3453
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 proper controls for the various elements in the XML file? I found an article that describes how to do it using stylesheets (http://www.dnzone.com/ShowDetail.asp?NewsId=151) but it's a little cumbersome and was tailored to .NET 1.1.
4
17409
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 creating a basic drawing with two shapes connected? I can then expand that example to include other elements. Also, my plan is for this project to be an ASP.Net website. Is Visio required to be on the web server in order to generate the Visio...
2
6540
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 trying to create a db for a friend's business. I have two issues that I'm wrestling with. First is how to dynamically create a text box on a form. I'm building a form that relates information between two tables (A, B). Table B is the source of...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.