troubleshooting Question

Unable to traverse child node using jdom xml parser

Avatar of aseshu70
aseshu70 asked on
XML
3 Comments1 Solution946 ViewsLast Modified:
I am using jdom for parsing my xml.
Below are the sample xml :
<PARAM BR="JUKE">
<SUBOR DT="2005-08-13 15:30:34" />
<DETAILS NO="300" >
   <DR="H4"/>
   <DR="G5"/>
   <DR="K8"/>
</DETAILS>
</MESSAGE>
I can able to traverse from the root element PARAM and read its children
SUBOR AND DETAILS. But DETAILS has got 'N' number of childrens
DR..DR.... Can anybody help me how to read subchildrens .

Below are my jdom java code:
import java.io.IOException;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;



//main class. Does everything
class  parseAFile
{
  public static void main(String[] args) throws Exception
  {
      //condition to check the use of input file name on command line  
      if (args.length == 0)
      {
            System.out.println("Usage: java filename.xml");
            return;
      }
      
      //Initialising the class which has the underlying XML parser
      SAXBuilder builder = new SAXBuilder();
      try
      {
         //Build the JDOM tree from the user entered XML file
        Document myDoc = builder.build(args[0]);

        //Get the root element of that file
        Element rootEle = myDoc.getRootElement();
        System.out.println("type" + rootEle.getAttributeValue("BR"));
        //Get all the children under this root into a list
        List rootChildren = rootEle.getChildren();

        //Use an iterator to manage this list
      Iterator iterator = rootChildren.iterator();

        //Traverse through the list and process each child
        while (iterator.hasNext())
        {
            Element child = (Element) iterator.next();

            //Check for the name of the child and do necessary processing based on this
            if( (child.getName()).equalsIgnoreCase("SUBOR") )
            {
            
              System.out.println("DATETIME" + child.getAttributeValue("DT"));
             
              }
            else if( (child.getName()).equalsIgnoreCase("DETAILS") )
            {
            
              System.out.println(" NO" + child.getAttributeValue("NO"));
             }
            else
              System.out.println("No Match!");
            }
        }
        catch (JDOMException e)
        {
            System.out.println(args[0] + " is not well-formed.");
            System.out.println(e.getMessage());
        }  
  }
}

Pls help
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros