[jdom-interest] Searching an XML file

Q.M.Qasim at Bradford.ac.uk Q.M.Qasim at Bradford.ac.uk
Wed Feb 13 06:55:23 PST 2002

I want to search an XML file with a search query. Following is a sample of my 
xml file.

<file name="fan.txt" directory="directory" size="8">

<file name="qasim" directory="directoru" size="3">
<description> qa</description>

I am trying to search for every <description> element that may contain content 
such as 'qas'. I wrote the following method, but there are some potential 
problems with it. (1) It only searches the first element of <file> and never 
goes to the second <file> element. (2) It only searches the first <description> 
element of <file> element and never goes to the second <description> element. 

If I try to search for 'qas', then I get the following error:

name is "fg" , directory is "directory", size is" 8"
Exception in thread "main" java.lang.NullPointerException
at Jdom_files.searchFiles(Jdom_files.java:154)
at Jdom_files.main(Jdom_files.java:193)

I will be very grateful if someone can tell what i may be doing wrong and if 
possible provide me with code showing how I can search through the element. My 
method is as follows:

public String searchFiles(String search)
	 String[] none= {"No Files Found"};
	// defaultTable = table;   
	 Element root = document.getRootElement();   
	 List files = root.getChildren("file");   
	 Iterator i=files.iterator();   
	 while (i.hasNext()) 
	 Element file= (Element)i.next();      
	 int count=0;
	 //// set up loop to read description//      
	 String description = file.getChild("description").getText();  
	 //if (search.equals(description)) 
	  int q = search.compareTo(description);
	 if (q==0)  
	 String name=file.getAttributeValue("name");         
	 String directory =file.getAttributeValue("directory");        
	 String size = file.getAttributeValue("size");         
	 String[] data= {name,directory,size};         
	 System.out.println( "name is " +name +" directory is "+ directory +" 
size is "+ size);
	 count++;   // increment number of description tag found      
	 }//// end of loop//      
	 else if (count==0) 
	 {   // if no description tag found         
	 //String[] none= {"No Files Found"};         
	 return null;      


This mail sent through IMP: webmail.brad.ac.uk

More information about the jdom-interest mailing list