Help with DOM.I am trying to access the nodes acc. to their levels in an xml file.reached upto 3-4 levels then getting a nullpointer exception.Please help...wat datastructure would better suit this.i need to index all the nodes.the xml file is cs.xml given under and the java code below tht.

<department>
<deptname>Computer Sciences</deptname>
    <gradstudent>
    <name>
      <lastname>Aboulnaga</lastname>
      <firstname>Ashraf</firstname>
    </name>
    <phone>2626623</phone>
    <email>ashraf@cs.wisc.edu</email>
    <address>
      <city>Madison</city>
      <state>WI</state>
      <zip>53705</zip>       
    </address>
    <office>7360</office>
    <url>www.cs.wisc.edu/~ashraf</url>
    <gpa>4.0</gpa>
  </gradstudent>

  <gradstudent>
    <name>
      <lastname>Ailamaki</lastname>
      <firstname>Anastasia</firstname>
    </name>
    <phone>2652311</phone>
    <email>natassa@cs.wisc.edu</email>
    <address>
      <city>Madison</city>
      <state>WI</state>
      <zip>53706</zip>       
    </address>
    <address>
      <city>Madison</city>
      <state>WI</state>
      <zip>53706</zip>       
    </address>

    <office>7351</office>
    <url>www.cs.wisc.edu/~natassa</url>
    <gpa>4.0</gpa>
  </gradstudent>
  </department>


import org.w3c.dom.*;
import org.w3c.dom.Element.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
public class project1{
  public static void main(String[] args) {
    try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter xml file name: ");
      String str = bf.readLine();
      String child[][]=new String[100][100];
      String parent[]=new String[100];
      String node[]=new String[100];
      String arr[][]=new String[100][100];
      NodeList fstNm1[][]=new NodeList[100][100];
      File file = new File(str);
      if (file.exists()){
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fact.newDocumentBuilder();
        Document doc = builder.parse(str);
        NodeList list = doc.getElementsByTagName("*");
          int num=0,p1=num,p2=num;
          //L-0
          Element element = (Element)list.item(0);//root node
          node[num]=element.getNodeName();
          Node n=element.getParentNode();//root's parent node #doc
          parent[num]=n.getNodeName();
          NodeList fstNm = element.getChildNodes();//childs of root
          System.out.println("Node no: "+(num)+ " has name " +element.getNodeName()+" parent name "+n.getNodeName()+" and no.of childs is "+(fstNm.getLength()-1)/2);
          for(int j=1,k=0;j<fstNm.getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm.item(j).getNodeName()+" Parent: "+fstNm.item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm.item(j).getNodeName();
          }
          num++;
          p1=num;
          System.out.println((fstNm.getLength()-1)/2);
          //L-1
          for(int i=1,y=0;i<fstNm.getLength();i+=2,y++)
          {
          Element element1 = (Element)fstNm.item(i);
          node[num]=element1.getNodeName();
          Node n1=element1.getParentNode();
          parent[num]=n1.getNodeName();
          fstNm1[1][y] = element1.getChildNodes();
          System.out.println("Node no: "+num+ " has name " + element1.getNodeName()+" parent name "+n1.getNodeName()+" and no.of childs is "+(fstNm1[1][y].getLength()-1)/2);
          if((fstNm1[1][y].getLength()-1)/2==0)
          {
          child[num][0]="No Childs";
          }
          else
          {
          for(int j=1,k=0;j<fstNm1[1][y].getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm1[1][y].item(j).getNodeName()+" Parent: "+fstNm1[1][y].item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm1[1][y].item(j).getNodeName();
          }
          }
          num++;
          }
          p2=num;
        //L-2
          for(int y=0;p1<p2;p1++,y++)
          {
          for(int i=1;i<fstNm1[1][y].getLength();i+=2)
          {
          Element element2 = (Element)fstNm1[1][y].item(i);
          node[num]=element2.getNodeName();
          Node n2=element2.getParentNode();
          parent[num]=n2.getNodeName();
          fstNm1[2][y] = element2.getChildNodes();
          System.out.println("Node no:"+num+ " has name " + element2.getNodeName()+" parent name: "+n2.getNodeName()+" and no.of childs is "+(fstNm1[2][y].getLength()-1)/2);
          if((fstNm1[2][y].getLength()-1)/2==0)
          {
          child[num][0]="No Childs";
          }
          else
          {
          for(int j=1,k=0;j<fstNm1[2][y].getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm1[2][y].item(j).getNodeName()+" Parent: "+fstNm1[2][y].item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm1[2][y].item(j).getNodeName();
          }
          }
          num++;
          }
          }
          p2=num;
        //L-3
          for(int y=0;p1<p2;p1++,y++)
          {
          for(int i=1;i<fstNm1[2][y].getLength();i+=2)
          {
          Element element2 = (Element)fstNm1[2][y].item(i);
          node[num]=element2.getNodeName();
          Node n2=element2.getParentNode();
          parent[num]=n2.getNodeName();
          fstNm1[3][y] = element2.getChildNodes();
          System.out.println("Node no: "+num+ " has name: " + element2.getNodeName()+" parent name: "+n2.getNodeName()+" and no.of childs is "+(fstNm1[3][y].getLength()-1)/2);
          if((fstNm1[3][y].getLength()-1)/2==0)
          {
          child[num][0]="No Childs";
          }
          else
          {
          for(int j=1,k=0;j<fstNm1[3][y].getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm1[3][y].item(j).getNodeName()+" Parent: "+fstNm1[3][y].item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm1[3][y].item(j).getNodeName();
          }
          }
          num++;
          }
          }
          for(int i=0;i<num;i++)
          {
          System.out.println("Node name is "+node[i]+" Parent Name is "+parent[i]+" Child names are ");
          for(int j=0;j<100;j++)
          {
          if((child[i][j]!=null))
          System.out.println(child[i][j]+(i)+" "+j);
          }
          }
      }
      else{
        System.out.println("File not found!");
      }
    }
    catch(Exception e){
            System.out.println("Exception "+e);}
  }
}

I'm sorry I'm not willing to go through that code it does look very messy. Have look at Processing XML with Java you do not necessary have to read it whole, you can simply start with JDOM which is very straight forward by my opinion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.