I have written the following code to extract the xml data using JDOM9.0
One of my collegues has an opinion to store the data in HashTable and use Map and they have the opinion of improving the performance.
Can any expert review and give his opinion?
Thanks in advance to all the cows & Bar tenders
<<<<<<<<<<<<<< ------ CODE STARTS HERE ---------------->>>>>>>>>>>>>>>>>>>>
import java.io.*;
import java.util.List;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class ZipExtractor {
public static void main(
String[] args) {
ZipExtractor ze=new ZipExtractor();
/* The xmlfile and myzip are at present taken as args[0] and args[1] from the command line however
they can be passed on to the extractZip method as arguments when called from a seperate class */
String xmlfile=args[0];
String myzip=args[1];
String stationID=ze.extractZip(myzip,xmlfile);
System.out.println("stationID from main method is: "+stationID);
}
public String extractZip(String myzip,String xmlfile){
String station="Entered a wrong or unavailable zip code";
try {
// Build the document with SAX and Xerces, no validation
SAXBuilder builder = new SAXBuilder();
// Create the document
Document
doc = builder.build(new File(xmlfile));
//Create an element
Element ziproot=doc.getRootElement();
List childElements = ziproot.getChildren();
//Create a list of child elements
Iterator i =childElements.iterator();
while(i.hasNext()){
Element ce=(Element)i.next();
/*ce.getText() gets the value in child element and compares to the zip we want
to compare and if it exists and furhter proceeds*/
if(ce.getText().equals(myzip)){
System.out.println("Your zipcode: "+ce.getText()+" exists");
/*this is printing to the dos prompt the attribute value i.e the stattion name by the method
ce.getAttributeValue("stationID").stattionID is the attribute for the ZIPCode child element of
ZIPCodes root element.*/
station=ce.getAttributeValue("stationID");
System.out.println("stationID is: "+station);
}
}
} catch (Exception e) {e.printStackTrace();}
return station;
}
}