Hi,
I would like to traverse a XML document in levelorder. My next steps
depend on different nodes and their levels.
What I did by now was to set up a XmlStreamEventReader to easily
choose the next steps for each node and node-type:
m_in = new FileInputStream(file);
m_factory = XMLInputFactory.newInstance();
m_parser =
m_factory.createXMLEventReader(m_in);
// parse events
while(this.m_parser.hasNext())
{
XMLEvent event = m_parser.nextEvent();
switch(event.getEventType())
{
case
XMLStreamConstants.START_DOCUMENT:
break;
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
// common mib files
if(startElement.getName().toString()=="Common")
newCommonInstance(startElement);
....
The XmlStreamEventReader traverses the XML file in preorder.
And because of my XML file only consists of nodes with attributes (so
there's no node-value only attributes an their values), working with a
DOM is pretty strange. As soon as I walk through the nodes my root has
i.e. 5 instead of actually 2 children:
root
#text
node1
#text
node2
#text
How do I get rid of the "#text"-blanks or what should I do to
correctly traverse in levelorder?
|
|
0
|
|
|
|
Reply
|
ISO
|
12/5/2007 1:51:03 PM |
|
Okay, I just started over from scratch. I'm parsing the file now into
a DOM and give the XML root node to a method like shown here:
private void visitNodes(Node node)
{
try
{
// child nodes of current node
NodeList nodeList = node.getChildNodes();
// loop through child nodes
for(int i=0; i<nodeList.getLength(); i++)
{
if(nodeList.item(i).getLocalName()=="Component")
{
System.out.println(nodeList.item(i).getLocalName());
}
}
}
catch(Exception ex)
{
// nothing yet
}
}
But XML level-order traversal ain't that easy... How and where can I
now insert a recursive call of that method? I can't just put it into
the for-loop, because then I get pre-ordered results. So what should I
do to traverse my file in level-order (breadth-first)?
I hope that someone gives me a little help here. Thanks a lot!
The XML file looks a little like this:
<Component>
<Component>
<Component/>
<Component/>
</Component>
<Component/>
</Component>
|
|
0
|
|
|
|
Reply
|
ISO
|
12/6/2007 10:16:40 AM
|
|
Solution found:
Queue q = new Queue();
q.enqueue(node);
while(!q.isEmpty())
{
node = (Node) q.dequeue();
if(node.getLocalName()=="TopLevelComponent"
|| node.getLocalName()=="Component"
|| node.getLocalName()=="SubComponent")
{
System.out.println(node.getAttributes().item(2).getNodeValue() +
m_mncn);
m_mncn++;
}
NodeList children = node.getChildNodes();
for(int i=0; i<children.getLength(); i++)
q.enqueue(children.item(i));
}
|
|
0
|
|
|
|
Reply
|
ISO
|
12/6/2007 12:38:41 PM
|
|
C. Rühl wrote:> Solution found:> > Queue q = new Queue();> q.enqueue(node);> > while(!q.isEmpty())> {> node = (Node) q.dequeue();> > if(node.getLocalName()=="TopLevelComponent"> || node.getLocalName()=="Component"> || node.getLocalName()=="SubComponent")> {> System.out.println(node.getAttributes().item(2).getNodeValue() +> m_mncn);> m_mncn++;> }> > NodeList children = node.getChildNodes();> > for(int i=0; i<children.getLength(); i++)> q.enqueue(children.item(i));> }Thanks for sharing the solution, but two pieces of advice.Do not multi-post. Use cross-posting, as I did, if you must reach more than one group, but cross-posting between clj.help and clj.programmer is redundant. Certainly never multi-post.The post entitled "comp.lang.java.{help,programmer} - what they're for (mini-FAQ 2006-03-31)" by David Alex Lamb that's posted here every five days has links about newsgroup ettiquette that explain this in detail.The other piece of advice is to use spaces, not TABs, to indent in Usenet postings. As you can see, TABs really do not make good use of space here.-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
12/6/2007 1:02:13 PM
|
|
> Thanks for sharing the solution, but two pieces of advice.
>
> Do not multi-post. Use cross-posting, as I did, if you must reach more than
> one group, but cross-posting between clj.help and clj.programmer is redundant.
> Certainly never multi-post.
>
> The post entitled "comp.lang.java.{help,programmer} - what they're for
> (mini-FAQ 2006-03-31)" by David Alex Lamb that's posted here every five days
> has links about newsgroup ettiquette that explain this in detail.
>
> The other piece of advice is to use spaces, not TABs, to indent in Usenet
> postings. As you can see, TABs really do not make good use of space here.
>
> --
> Lew
Yeah, you're right and thanks for the advice! I'll be checking out the
FAQ and try my best to follow the newsgroup ettiquettes from now on. I
just didn't know... Sorry for that.
|
|
0
|
|
|
|
Reply
|
ISO
|
12/6/2007 1:09:58 PM
|
|
C. Rühl wrote:
...
>Yeah, you're right and thanks for the advice! I'll be checking out the
>FAQ and try my best to follow the newsgroup ettiquettes from now on.
With that kind of attitude/approach, I guess you will end up
getting good help from people on usenet.
>...I just didn't know...
It takes a wise person to recognise/admit that. But
'take heart' - T.A.E. is 'right there with you'..
<http://www.quotedb.com/quotes/1382>
..You are learning, and that is the important thing. ;-)
--
Andrew Thompson
http://www.physci.org/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-setup/200712/1
|
|
0
|
|
|
|
Reply
|
Andrew
|
12/6/2007 2:09:29 PM
|
|
Andrew Thompson wrote:
> C. Rühl wrote:
> ...
>> Yeah, you're right and thanks for the advice! I'll be checking out the
>> FAQ and try my best to follow the newsgroup ettiquettes from now on.
>
> With that kind of attitude/approach, I guess you will end up
> getting good help from people on usenet.
>
>> ...I just didn't know...
>
> It takes a wise person to recognise/admit that. But
> 'take heart' - T.A.E. is 'right there with you'..
> <http://www.quotedb.com/quotes/1382>
>
> ..You are learning, and that is the important thing. ;-)
And to repeat - I (and others) appreciate that the OP posted the solution when
they found it - we all want to learn, so seeing the answers is helpful to all.
The code sample was good for its purpose, too (once you factor out the TABs).
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
12/6/2007 2:13:19 PM
|
|
On 6 Dez., 15:13, Lew <l...@lewscanon.com> wrote:
> Andrew Thompson wrote:
> > C. R=FChl wrote:
> > ...
> >> Yeah, you're right and thanks for the advice! I'll be checking out the
> >> FAQ and try my best to follow the newsgroup ettiquettes from now on.
>
> > With that kind of attitude/approach, I guess you will end up
> > getting good help from people on usenet.
>
> >> ...I just didn't know...
>
> > It takes a wise person to recognise/admit that. But
> > 'take heart' - T.A.E. is 'right there with you'..
> > <http://www.quotedb.com/quotes/1382>
>
> > ..You are learning, and that is the important thing. ;-)
>
> And to repeat - I (and others) appreciate that the OP posted the solution =
when
> they found it - we all want to learn, so seeing the answers is helpful to =
all.
>
> The code sample was good for its purpose, too (once you factor out the TAB=
s).
>
> --
> Lew
There's a simple reason why I always post my solutions even though the
thread (like this one before) seems to be "ignored" or something:
I hate it so much when forum users finish threads with the words
"solution found" but not telling how they solved it. For example: When
you're searching for some error message you experienced, you'll find
millions problem descriptions that exactly match your problem - but
they never come with a solution at the end. That's so annoying...
By the way: I just found the mini-FAQ. ;-)
But okay, back to the problem... Well, maybe I should post a new
message for it's not really a traversal problem anymore. So I hope to
see you there.
|
|
0
|
|
|
|
Reply
|
ISO
|
12/7/2007 8:59:39 AM
|
|
|
7 Replies
136 Views
(page loaded in 0.539 seconds)
Similiar Articles: hash table quadratic probing help please - comp.lang.java ...XML traversal in levelorder - comp.lang.java.help quadratic probe help please 2 158 Totti ... Design question: "threaded view" in table component 1 180 luc (4) Re: Read in XML-file via URL - comp.soft-sys.sasWriting a outlook msg file using ... XML file using pfx file - comp.lang.java.security Currently I have the pfx file being read into using ... Binary Search Tree - Levelorder traversal - dynamic recursion ...... scripts in languages including PHP, C++, C, Perl, Java, Visual Basic, XML ... post-order traversal level-order traversal the first three are easy using recursive algorithms. Tree traversal - Wikipedia, the free encyclopediaPostorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root) Breadth-first. Level-order traversal sequence: F, B, G, A, D, I, C, E, H 7/18/2012 9:33:08 AM
|