import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.Namespace; import org.jdom.input.SAXBuilder; import org.jdom.input.DOMBuilder; import org.jdom.output.XMLOutputter; /** * Title: NamespacePerformance * Description: Illustrate the performance penalty of reading an XML document with Namespaces as opposed to a document * with no Namespaces in JDOM * Copyright: Copyright (c) 2001 * Company: iMedium * @author Ken Klose * @version 1.0 */ public class NamespacePerformance { public static final int SIZE = 20; public static void main(String[] args) throws IOException, JDOMException { int size = (args.length < 1) ? SIZE : Integer.parseInt(args[0]); System.out.println("NamespacePerformance test size = " + size); for(int i = 0; i < 4; i++) { Namespace exNS; String status; if(i % 2 == 0) { exNS = Namespace.getNamespace("ex","http://viu.imedium.com/xml/namespaces/ex"); status = "namespace EX"; } else { exNS = null; status = "NO namespace"; } System.out.println("\nCreating test Document in memory with " + status); Timer.start(); Document testDoc = createTestDoc(size, exNS); Timer.stop(); XMLOutputter xmlout = new XMLOutputter(" ", true); File testFile = new File("nsPerfTest" + i + ".xml"); FileOutputStream fos = new FileOutputStream(testFile); System.out.println("Writing test Document with " + status + " to " + testFile.getCanonicalPath()); Timer.start(); xmlout.output(testDoc, fos); Timer.stop(); fos.close(); testDoc = null; // Let the garbage collector free this up. System.out.print("Reading test Document with " + status + " from " + testFile.getCanonicalPath()); if(i < 2) { System.out.println(" with DOMBuilder."); DOMBuilder builder = new DOMBuilder(); Timer.start(); Document readDoc = builder.build(testFile); Timer.stop(); } else { System.out.println(" with SAXBuilder."); SAXBuilder builder = new SAXBuilder(); Timer.start(); Document readDoc = builder.build(testFile); Timer.stop(); } } } public static Document createTestDoc(int size, Namespace ns) { Element managersElt = new Element("salesmanagers",ns); // Root element for(int i = 0; i < size; i++) { Element managerElt = new Element("salesmanager", ns); // child or addSalesmanElements(managerElt, size, ns); // has children addCustomerElements(managerElt, size, ns); // has children managersElt.addContent(managerElt); // Add as child of } Document doc = new Document(managersElt); return doc; } /** * Add child elements to passed in element. */ public static void addSalesmanElements(Element elt, int size, Namespace ns) { for(int i = 0; i < size; i++) { Element salesassElt = new Element("salesassociate", ns); addCustomerElements(salesassElt, size, ns); // has children elt.addContent(salesassElt); // Add to parent Element } } public static void addCustomerElements(Element elt, int size, Namespace ns) { for(int i = 0; i < size; i++) { Element customerElt = new Element("customer", ns); elt.addContent(customerElt); // Add to parent Element } } } class Timer { private static long lastStart; public static void start() { lastStart = System.currentTimeMillis(); System.out.print(" Starting timer at: " + lastStart); System.out.flush(); } public static void stop() { long stopTime = System.currentTimeMillis(); System.out.println(" Stopped at: " + stopTime + " Elapsed: " + (stopTime - lastStart) + "ms."); System.out.flush(); } }