Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

XML traversal in levelorder

Thread view: 
C. Rühl - 05 Dec 2007 13:53 GMT
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?
C. Rühl - 06 Dec 2007 10:16 GMT
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>
C. Rühl - 06 Dec 2007 12:38 GMT
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));
            }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.