import org.w3c.dom.*; import java.io.*; import java.util.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import javax.xml.parsers.*; import javax.xml.xpath.*; public class TransformDOM { public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); byte[] bytes = new byte[(int)length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } is.close(); return bytes; } public static void main(String args[]) { test(true); test(true); test(true); test(true); test(true); test(true); test(true); test(true); test(true); test(true); } public static void test(boolean forReal) { long start; long end; int kk=10; double time = 0.0; int i=0; int j = 0; Document doc = null; XPathExpression xpath; Number number = new Integer(0); Object res = null; NodeList set = null; try { byte[] content = getBytesFromFile(new File("blog.xml")); ByteArrayInputStream bais = new ByteArrayInputStream(content); ByteArrayOutputStream baos = new ByteArrayOutputStream(content.length * 2); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); start = System.currentTimeMillis(); for (int ii=0; i < kk; i++) { doc = builder.parse(bais); bais.reset(); xpath = XPathFactory.newInstance().newXPath().compile("count(//*)"); res = xpath.evaluate(doc, XPathConstants.NUMBER); xpath = XPathFactory.newInstance().newXPath().compile("//*"); res = xpath.evaluate(doc, XPathConstants.NODESET); xpath = XPathFactory.newInstance().newXPath().compile("/blog/item"); res = xpath.evaluate(doc, XPathConstants.NODESET); xpath = XPathFactory.newInstance().newXPath().compile("//text()"); res = xpath.evaluate(doc, XPathConstants.NODESET); xpath = XPathFactory.newInstance().newXPath().compile("count(//item)"); res = xpath.evaluate(doc, XPathConstants.NUMBER); xpath = XPathFactory.newInstance().newXPath().compile("count(/blog/item)"); res = xpath.evaluate(doc, XPathConstants.NUMBER); xpath = XPathFactory.newInstance().newXPath().compile("/blog/item[@num='a781']"); set = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET); for (int r=set.getLength(); r > 0;) { Node node = set.item(--r); node.getParentNode().removeChild(node); } xpath = XPathFactory.newInstance().newXPath().compile("/blog/item/body/p/a"); set = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET); for (int r=set.getLength(); r > 0;) { Node node = set.item(--r); node.getParentNode().removeChild(node); } Source source = new DOMSource(doc); Result result = new StreamResult(baos); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); baos.reset(); } end = System.currentTimeMillis(); System.err.println("average cycle time (ms): " + (end - start) / kk); } catch (Exception ie) { ie.printStackTrace(); } } }