• 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

XML data extraction code performance....

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}


}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this routine will be called more than once, then yes, parsing the document only once and storing the results in a Map, then looking the results up in the Map, will be much faster than parsing the whole XML document repeatedly, each time you need to look up a single value.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest for ur valuable feed back
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Earnst

Hereunder I am pasting the new code written using the Hashtable.U may pl review and give ur expert views..

<<<<<<<<<<<-----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 ZipExtractor2 {
public static void main(String[] args) {
/* 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];

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();
Hashtable zipdata=new Hashtable();
//Create a list of child elements
Iterator i =childElements.iterator();
while(i.hasNext()){
Element ce=(Element)i.next();
zipdata.put(ce.getText(),ce.getAttributeValue("stationID"));
}
String stationID=extractZip(myzip,zipdata);
System.out.println("stationID is: "+stationID);
} catch (Exception e) {e.printStackTrace();}
}

public static String extractZip(String myzip,Hashtable zipdata){
String station="Entered a wrong or unavailable zip code";
if(zipdata.containsKey(myzip)){
station=(String)zipdata.get(myzip);
}
return station;
}

}
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic