public class XMLTest extends DefaultHandler {
/**
* @param args
*/
Stack
public XMLTest() {
super();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
XMLTest myXML = new XMLTest();
System.out.println("=====================DOM=========================");
myXML.DOM();
System.out.println("=====================SAX=========================");
myXML.SAX();
System.out.println("=====================JDOM========================");
myXML.JDOM();
System.out.println("=====================DOM4J=======================");
myXML.DOM4J();
System.out.println("=================================================");
}
File f = new File("test.xml");
public void DOM() {
long start = System.currentTimeMillis();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("node");
for (int i = 0; i < nl.getLength(); i++) {
System.out.println("Name|"
+ doc.getElementsByTagName("name").item(i)
.getFirstChild().getNodeva lue());
System.out.println("Space|"
+ doc.getElementsByTagName("space").item(i)
.getFirstChild().getNodeva lue());
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("dom runtime" + (end - start) + "MS");
}
public void SAX() {
long start = System.currentTimeMillis();
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XMLTest reader = new XMLTest();
sp.parse(f, reader);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("sax runtime"
+ (System.currentTimeMillis() - start) + " MS");
}
public void startElement(String uri, String localName, String qName,
Attributes attrs) {
tags.push(qName);
}
public void characters(char ch[], int start, int length)
throws SAXException {
String tag = (String) tags.peek();
if (tag.equals("name")) {
System.out.println("Name|" + new String(ch, start, length));
}
else if (tag.equals("space")) {
System.out.println("Space|" + new String(ch, start, length));
}
}
public void JDOM() {
long start = System.currentTimeMillis();
try {
SAXBuilder builder = new SAXBuilder();
org.jdom.Document doc = builder.build(f);
Element foo = doc.getRootElement();
List< > allChaildren = foo.getChildren();
for (int i = 0; i < allChaildren.size(); i++) {
System.out.println("Name|"
+ ((Element) allChaildren.get(i)).getChild("name")
.getText());
System.out.println("Space"
+ ((Element) allChaildren.get(i)).getChild("space")
.getText());
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("jdom runtime" + (end - start) + "MS");
}
public void DOM4J() {