• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using XML in search criteria rather than hard coding it

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following program:

import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class FindFile{

public static void main (String args []){

try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("Files.xml"));

// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is : " +
doc.getDocumentElement().getNodeName());

//number of files
NodeList fileDetails = doc.getElementsByTagName("file_details");
int totalFiles = fileDetails.getLength();
System.out.println("Total no of files : " + totalFiles);

for(int s=0; s<fileDetails.getLength() ; s++){


Node countryNode = fileDetails.item(s);
if(countryNode.getNodeType() == Node.ELEMENT_NODE){


Element fileElement = (Element)countryNode;

//Get the country each file applys to
NodeList countryList = fileElement.getElementsByTagName("country");
Element countryNameElement = (Element)countryList.item(0);

NodeList textCountryList = countryNameElement.getChildNodes();
System.out.println("Country Name : " +
((Node)textCountryList.item(0)).getNodeValue().trim());

//Get the letter each file begins with
NodeList fileBeginsWith = fileElement.getElementsByTagName("starts_with");
Element lastNameElement = (Element)fileBeginsWith.item(0);

NodeList textBeginsWithList = lastNameElement.getChildNodes();
System.out.println("File Name Begins With : " +
((Node)textBeginsWithList.item(0)).getNodeValue().trim());

//find the extension type
NodeList extensionType = fileElement.getElementsByTagName("extension");
Element extensionElement = (Element)extensionType.item(0);

NodeList textExtensionList = extensionElement.getChildNodes();
System.out.println("The extension type is : " +
((Node)textExtensionList.item(0)).getNodeValue().trim());


}//end of if clause

}//end of for loop with s var

}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());

}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();

}catch (Throwable t) {
t.printStackTrace ();
}

//Was commented out
System.exit (0);
}
}



It reads the XML below:

<?xml version="1.0" encoding="UTF-8"?>

<file>
<file_details>
<country>Ireland</country>
<starts_with>A</starts_with>
<extension>.txt</extension>
<message>Irish Financial Regulator</message>
</file_details>
</file>

I also have this program which finds the latest modified file in a folder:


import java.io.File;


public class FindLatestFile {


public static File getLatest(File thisDir){
long latestModDate = -1;
File latestFile = null;
File[] fileList = thisDir.listFiles();
for(int i=0; i >< fileList.length; i++){
File file = fileList[i];
if (file.lastModified() > latestModDate & (file.getName().startsWith("A") & file.getName().endsWith(".txt"))) {
latestModDate = file.lastModified();
latestFile = file;
}
}
return latestFile;

}
}


What I want to do is to use my FindLatestFile program but to in some way incorporate my code from FindFile so that instead of hard coding the search criteria I want to include in my search, the criteria will be read from the XML.

I hope that is clear. If not please free to ask questions.

Any help would be greatly appreciated.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Creating search expressions dynamically from text input sounds to me like a job for the XPath API. Java standard library since 1.5 contains the javax.xml.xpath package which provides for evaluation of XPath expressions.

I wrote this introductory article on XPath in Java 1.5.

E. Harolds chapter on using XPath is available online.

Bill
 
Paul Carron
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers. Sorted now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic