build a tree in XML

E

E

Hi everyone,

I'm new to XML and I'm just wondering if someone could point me to some
tutorials on how to generate an XML file using PHP? I've been Googling for
a few hours and I've yet to find anything that matches what I'm trying to
do. Maybe I'm not using the correct terms??? :)

What I'm trying to do is to use data from the MySQL table below to generate
an XML file which I will then use to display that info as a hierarchy.

+----+-----------+------------------+
| id | parent_id | name |
+----+-----------+------------------+
| 1 | NULL | about us |
| 2 | NULL | products |
| 3 | NULL | contact us |
| 4 | 2 | product1 |
| 5 | 2 | product2 |
| 6 | 2 | product3 |
| 7 | 4 | category1.1 |
| 8 | 4 | category1.2 |
| 9 | 4 | category1.3 |
| 10 | 5 | category2.1 |
| 11 | 5 | category2.2 |
| 12 | 6 | category3.1 |
| 13 | 7 | subcategory1.1.1 |
| 14 | 7 | subcategory1.1.2 |
+----+-----------+------------------+


Expected result:

- About Us
- Products
- Product 1
- Category 1.1
- SubCategory 1.1.1
- SubCategory 1.1.2
- Category 1.2
- Category 1.3
- Product 2
- Category 2.1
- Category 2.2
- Product 3
- Category 3.1
- Contact Us

First of all, how do I arrange the above data into an XML file and then, how
do I read the data from the file and display it like the list above?

TIA
Eric
 
J

Joe Kesselman

I'm not sure what question you're asking.

All XML documents are hierarchically structured, and thus can be
considered as trees. Tree nodes can generally be mapped fairly directly
to XML elements.

Building and accessing XML from an arbitrary program is generally done
with one of the standard APIs such as SAX or DOM, plus parsers and
serializers which speak those APIs. These tools are widely available.

Rendering XML content into a displayed form is generally done either by
special-purpose code (in which case, see previous paragraph) or using an
XSLT stylesheet.
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

E said:
What I'm trying to do is to use data from the MySQL table below to generate
an XML file which I will then use to display that info as a hierarchy.

Assuming that your data can be stored into
an ASCII file like this:

1 NULL about us
2 NULL products
3 NULL contact us
4 2 product1
5 2 product2
6 2 product3
7 4 category1.1
8 4 category1.2
9 4 category1.3
10 5 category2.1
11 5 category2.2
12 6 category3.1
13 7 subcategory1.1.1
14 7 subcategory1.1.2

Then you can use the following AWK script
to convert this to XML:

BEGIN { print "<?xml version=\"1.0\" ?>\n<root>" }

{ id=$1; parent[id] = $2; $1=$2=""; elem[id] =$0 }

END { out_xml("NULL", 0); print "</root>" }

function out_xml(i, d) {
for (id in parent) {
if (parent[id] == i) {
printf("%s%s\n", "<lev" d ">", elem[id])
out_xml(id, d+1)
printf "</lev" d ">"
}
}
}
First of all, how do I arrange the above data into an XML file and then, how
do I read the data from the file and display it like the list above?

The output of the script above for the given example
looks like this:

<?xml version="1.0" ?>
<root>
<lev0> about us
</lev0><lev0> products
<lev1> product1
<lev2> category1.1
<lev3> subcategory1.1.1
</lev3><lev3> subcategory1.1.2
</lev3></lev2><lev2> category1.2
</lev2><lev2> category1.3
</lev2></lev1><lev1> product2
<lev2> category2.1
</lev2><lev2> category2.2
</lev2></lev1><lev1> product3
<lev2> category3.1
</lev2></lev1></lev0><lev0> contact us
</lev0></root>
 
P

p.lepin

E said:
I'm new to XML and I'm just wondering if someone could
point me to some tutorials on how to generate an XML file
using PHP?

What I'm trying to do is to use data from the MySQL table
below to generate an XML file which I will then use to
display that info as a hierarchy.

[relational data]
First of all, how do I arrange the above data into an XML
file

It's very straightforward:

<?php

error_reporting ( E_ALL ) ;

$data =
array
(
array
(
'id' => 1 ,
'parent_id' => NULL ,
'name' => 'about us' ,
) ,
/* *************************
* the rest of your data *
************************* */
) ;

function genxml ( $data , $parent = NULL )
{
$result = '' ;
foreach ( $data as $element )
{
if ( $element [ 'parent_id' ] == $parent )
{
$element [ 'name' ] =
preg_replace
(
'/\s+/' ,
'_' ,
$element [ 'name' ]
) ;
$element [ 'no' ] =
preg_replace
(
'/^.*?(\d*)$/' ,
' no="\1"' ,
$element [ 'name' ]
) ;
$element [ 'name' ] =
preg_replace
(
'/[\d\.]+$/' ,
'' ,
$element [ 'name' ]
) ;
$result .=
'<' . $element [ 'name' ] .
(
$element [ 'no' ] != ' no=""'
? $element [ 'no' ]
: ''
) .
'>' ;
$result .= genxml ( $data , $element [ 'id' ] ) ;
$result .= '</' . $element [ 'name' ] . '>' ;
}
}
return $result ;
}

header ( 'Content-Type: text/xml' ) ;

print
(
'<?xml-stylesheet type="text/xsl" href="genxml.xsl"?>'
) ;
print ( '<data>' ) ;
print ( genxml ( $data ) ) ;
print ( '</data>' ) ;

?>
and then, how do I read the data from the file and
display it like the list above?

The following transformation (referred above as
'genxml.xsl') does that:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="text"/>
<xsl:template match="/data">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xsl:param name="indent" select="''"/>
<xsl:value-of select="$indent"/>
<xsl:text>- </xsl:text>
<xsl:apply-templates select="." mode="get-name"/>
<xsl:if test="@no">
<xsl:call-template name="get-no"/>
</xsl:if>
<xsl:text>
</xsl:text>
<xsl:apply-templates>
<xsl:with-param
name="indent"
select="concat($indent,' ')"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="about_us" mode="get-name">
<xsl:text>About Us</xsl:text>
</xsl:template>
<xsl:template match="products" mode="get-name">
<xsl:text>Products</xsl:text>
</xsl:template>
<xsl:template match="contact_us" mode="get-name">
<xsl:text>Contact Us</xsl:text>
</xsl:template>
<xsl:template match="product" mode="get-name">
<xsl:text>Product</xsl:text>
</xsl:template>
<xsl:template match="category" mode="get-name">
<xsl:text>Category</xsl:text>
</xsl:template>
<xsl:template match="subcategory" mode="get-name">
<xsl:text>SubCategory</xsl:text>
</xsl:template>
<xsl:template name="get-no">
<xsl:text> </xsl:text>
<xsl:apply-templates select=".." mode="get-no"/>
<xsl:value-of select="@no"/>
</xsl:template>
<xsl:template match="*[not(@no)]" mode="get-no"/>
<xsl:template match="*[@no]" mode="get-no">
<xsl:apply-templates select=".." mode="get-no"/>
<xsl:value-of select="@no"/>
<xsl:text>.</xsl:text>
</xsl:template>
</xsl:stylesheet>

Figuring out how does all of that work is left as a
(potentially enlightening) exercise for the reader.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top