Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 10th, 2006, 03:35 AM
E
Guest
 
Posts: n/a
Default build a tree in XML

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




  #2  
Old December 10th, 2006, 05:55 AM
Joe Kesselman
Guest
 
Posts: n/a
Default Re: build a tree in XML

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
  #3  
Old December 10th, 2006, 12:05 PM
Jürgen Kahrs
Guest
 
Posts: n/a
Default Re: build a tree in XML

E wrote:
Quote:
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 ">"
}
}
}
Quote:
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>
  #4  
Old December 11th, 2006, 09:25 AM
p.lepin@ctncorp.com
Guest
 
Posts: n/a
Default Re: build a tree in XML


E wrote:
Quote:
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]
Quote:
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>' ) ;

?>
Quote:
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:output 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.

--
Pavel Lepin

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles