Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > build a tree in XML

Reply
Thread Tools

build a tree in XML

 
 
E
Guest
Posts: n/a
 
      12-10-2006
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




 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      12-10-2006
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      12-10-2006
E wrote:

> 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>
 
Reply With Quote
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      12-11-2006

E wrote:
> 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="text"/>
<xsl:template match="/data">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xslaram 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.

--
Pavel Lepin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create XML tree from another partial tree Peter Reimer XML 4 04-12-2010 10:06 PM
using expat parser how to build a tree shaped data structure of a xml document and read the value from it in c language sharan XML 4 11-05-2007 08:59 AM
insert E4X XML tree inside existing DOM tree Joris Gillis XML 2 06-16-2006 08:30 PM
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments