473,394 Members | 1,831 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,394 software developers and data experts.

build a xml tree recursively

Hi,

I have the following records stored in the database :

TableName: Folder { FolderID, FolderName, TopLevel}

TableName: FolderRelationship { ParentID, ChildID}

This describes a folder tree structure. Note that only one record has
TopLevel set to 1, this is the top folder in the structure.

I get the records in two datatables. Then I would like to build the
following xml based on the records :

<node folderid=1 foldername="top folder">
<node folderid=2 foldername "firstchild" />
<node folderid=3 foldername "secondchild">
<node folderid=4 foldername="thirdchild" />
</node>
</node>

which would correspond to the following tree:

top folder
|-firstchild
|-secondchild
|-thirdchild

How can I build such a tree recursively ?

Thank you

Jan 24 '07 #1
2 2325
graphicsxp wrote:
TableName: Folder { FolderID, FolderName, TopLevel}

TableName: FolderRelationship { ParentID, ChildID}
This describes a folder tree structure. Note that only one record has
TopLevel set to 1, this is the top folder in the structure.
How can I build such a tree recursively ?
Locate the (one and only) root "node", then process it and its child
"nodes" using a recursive function, something like this (air code)...

Sub SaveTree( _
folderId as Integer? _
, folderName as String
, sw as StreamWriter _
)
' Save the current Node
sw.WriteLine( "<node" _
& " folderid='" & folderId & "'" _
& " foldername='" & folderName & "'" _
& ">" )

' Now locate any child "nodes"
Dim ds as DataSet = GetChildrenOf( folderId )
If ds.Tables(0).Rows 0 Then
' Loop through and save child nodes
For Each dr as DataRow In ds.Tables(0).Rows
SaveTree( CInt?(dr.Item("ChildId")) _
, dr.Item("Name").ToString() _
, sw )
Next
End If

sw.WriteLine( "</node>" )
End Sub

.... then ...

Dim sw as New streamwriter( "file" )
SaveTree( rootId, rootName, sw )

HTH,
Phill W.
Jan 24 '07 #2
On Jan 24, 4:35 am, "graphicsxp" <samuelberthe...@googlemail.com>
wrote:
>
TableName: Folder { FolderID, FolderName, TopLevel}
TableName: FolderRelationship { ParentID, ChildID}
Any particular reason you have it in two tables? You could do it with
only a single table:

TableName: Folder {FolderId, FolderName, ParentId}

Then, the top level folder will be identified by having a NULL value
for ParentId. And a folder can find its child folders by looking for
all folders with a parentId equal to its own id.
top folder
|-firstchild
|-secondchild
|-thirdchild
Can a folder have only one child or multiple children?

Chris

Jan 24 '07 #3

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

Similar topics

10
by: Sims | last post by:
Hi I have a table with something like ID PARENT 0 | -1 1 | -1 2 | 1 3 | 1
7
by: Max M | last post by:
I guess that the best approach is calling a shell tool with something like os.popen(). But I cannot seem to find any free tools. Winzip has a command line option, but for registered users only....
8
by: ted | last post by:
How does the speed of the Scons build tool compare with Ant? Right now with out Ant builds take around an hour. Hoping to speed that up. TIA, Ted
0
by: Dinesh | last post by:
Hi, I have two tables 'master' and 'child', the master is the master table for all nodes in all trees. To get children of any node, we need to go to the 'child' table to get the nodeid of the...
25
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
10
by: Dan Nash | last post by:
Hi peeps.. Im using the TreeView control from IE COntrols to create a directory structure, and trying to do it recursively. The code works, but my question is simply how can I make the subdirs...
3
by: Yuri CHUANG | last post by:
Hi, Here is my code, #include<stdio.h> #include<stdlib.h> typedef struct BiTNode { char data; struct BiTNode *lchild,*rchild;
7
by: snitu | last post by:
hello i am new user of this forum. i have a problem in javascript code. plz. give me idea about how to create tree using xml file (not using xsl file) in javascript. i am also sending my...
3
by: FARAECHILIBRU | last post by:
i'm building a binary tree the problem is when i'm reading from e text file .First i'm sending to tree builder function a empty node and e new node the function return me firt node of a tree, but...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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...

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.