Create own data structure from DOM?  
Author Message
C. R黨l





PostPosted: 2007-12-7 17:22:00 Top

java-programmer, Create own data structure from DOM? Hi again!

I successfully parsed my XML file into a DOM and traversed it in level-
order. I did that to gather information about component number and
level of each component node in the tree, because I need to work with
that information a little later.

The original XML file looks a little like this (the attribute index
letters represent the level-order "numbering"):

<Component ind="A">
<Component ind="B">
<Component ind="D"/>
<Component ind="E"/>
</Component>
<Component ind="C">
<Component ind="F"/>
</Component>
</Component>

What I'm trying to do now is to create instances of each component. I
have a Component class carrying all information I need - except one:
Who's my father component?

What should I do to tell every component instance of it's father node
without creating more than one instance of each component?

I thought of two and a half possibilities yet, but I really hope you
can help me here or give me some new ideas:

a) put every component node (from DOM) onto a stack and pop one by one
by creating component instances and create another component instance
of node.getParent(). but then I have a lot of component instances
doubled or tripled or even more... that ain't really efficient.

Stack:
F
E
D
C
B
A

b) create instances while traversing the DOM and add each instance to
a array list. But then I have the problem of not knowing the array
index of the father component.

Array:
A B C D E F

c) create multiple lists with array[0] carrying the father node
followed by it's child nodes (array[i])

Array 1:
A B C

Array 2:
B D E

Array 3:
C F

Thank you in advance!

/Chris
 
C. R黨l





PostPosted: 2007-12-7 21:34:00 Top

java-programmer >> Create own data structure from DOM? Oh, I am so ashamed!!! I really am!

Forget all this here. There is the simplest possible solution for
this: Create a temp object that holds the actual nodes instance and
append this to all its childs instances.

I'm so sorry for taxing your patience with this most stupid
questioning! I better think before I post.
But still it took me a while. Around 4 hours! Muahaha! x-D Okay, there
was lunch between but anyway... :-P

Thanks a lot! And bye for now!

/Chris
 
Lew





PostPosted: 2007-12-7 22:54:00 Top

java-programmer >> Create own data structure from DOM? C. R眉hl wrote:
> Oh, I am so ashamed!!! I really am!
>
> Forget all this here. There is the simplest possible solution for
> this: Create a temp object that holds the actual nodes instance and
> append this to all its childs instances.
>
> I'm so sorry for taxing your patience with this most stupid
> questioning! I better think before I post.
> But still it took me a while. Around 4 hours! Muahaha! x-D Okay, there
> was lunch between but anyway... :-P

It may have been a simple question, or had a simple answer, but that is a long
road away from being a stupid question.

I feel certain that if it really had been a stupid question I'd have thought
so immediately. Nope - I just thought about the question, what I might do,
and came up with something a little similar (vaguely) in my mind even as I
jumped down to your post wherein you smacked yourself in the forehead.

I also brought to my mind memories of similar XML parsing issues I've faced,
although in my case I was using SAX parsing and needed to have components
"remember" who their parents are. All in all, your simple little question did
me, at least, some bit of good.

I agree with you [*] wholeheartedly that posting the answer (even to such a
simple (-seeming) question) is a tremendous courtesy to the newsgroup. But

No need to reach for the sackcloth and ashes just yet. You are a terrific poster.

[*] Your point in another recent thread.

--
Lew
 
 
C. R黨l





PostPosted: 2007-12-7 23:57:00 Top

java-programmer >> Create own data structure from DOM? > It may have been a simple question, or had a simple answer, but that is a long
> road away from being a stupid question.

Okay, they say "there's no stupid question, only stupid answers. And
people not questioning are stupid." But this one could have been
solved easily with a little more thinking. :-D

> to your post wherein you smacked yourself in the forehead.

And it did hurt, believe me! :-P

> I also brought to my mind memories of similar XML parsing issues I've faced,
> although in my case I was using SAX parsing and needed to have components
> "remember" who their parents are. All in all, your simple little question did
> me, at least, some bit of good.

I was using a SaxParser and a XmlEventReader (which actually is
S(t)AX, isn't it?) before, too. But I did't find a way for a level-
order traversal, which - in this case - is essential to get every
components number and level. Well, maybe there is a chance to get this
using XPath but that might be a little overpowered for my purpose.

> No need to reach for the sackcloth and ashes just yet. You are a terrific poster.

In one of the XSLT usegroups they started *plonk*ing me, because I
didn't post SSCCE's. Okay, that was another thing I had to learn, but
I guess I did. At least a little.

What's for this solution: Currently I'm using that stack solution as
suggested in a). For each child node that is a component, I push the
current component instance to the stack. This is a little bohemian and
careless, but it's weekend time now. :-)
With these words, have a good one!
 
 
C. R黨l





PostPosted: 2007-12-8 18:26:00 Top

java-programmer >> Create own data structure from DOM? > What's for this solution: Currently I'm using that stack solution as
> suggested in a). For each child node that is a component, I push the
> current component instance to the stack. This is a little bohemian and
> careless, but it's weekend time now. :-)
> With these words, have a good one!

I just realized that I can't do this using a Stack. If I use a queue
to traverse the tree, then I have to use another queue to remember the
father nodes in the correct order.