Commits

ariovistus committed 1fbc82a Draft

hello xml!

Comments (0)

Files changed (6)

TransformDOM.java

+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();
+        }
+    }
+
+}

TransformDOM4J.java

+import java.io.*;
+import java.util.*;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamSource;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.Node;
+import org.dom4j.XPath;
+import org.dom4j.io.DOMWriter;
+import org.dom4j.io.DocumentResult;
+import org.dom4j.io.DocumentSource;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+
+public class TransformDOM4J {
+
+    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) {
+        Document doc;
+
+        long start;
+        long end;
+        XPath xpath;
+        int kk=10;
+        Number number = new Integer(0);
+        List nodeList = null;
+
+        try {
+            byte[] content = getBytesFromFile(new File("blog.xml"));
+            ByteArrayInputStream bais = new ByteArrayInputStream(content);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(content.length * 2);
+            XMLWriter writer = new XMLWriter(baos);
+            SAXReader reader = new SAXReader();
+            doc = reader.read(bais);
+            bais.reset();
+            XPath xpath1 = doc.createXPath("count(//*)");
+            XPath xpath2 = doc.createXPath("//item");
+            XPath xpath3 = doc.createXPath("/blog/item");
+            XPath xpath4 = doc.createXPath("//text()");
+            XPath xpath5 = doc.createXPath("count(//item)");
+            XPath xpath6 = doc.createXPath("count(/blog/item)");
+            XPath xpath7 = doc.createXPath("/blog/item/body/p/a");
+
+            start = System.currentTimeMillis();
+
+            for (int ii = 0; ii < kk; ii++) {
+                //parse
+                doc = reader.read(bais);
+
+                number  = xpath1.numberValueOf(doc);
+                nodeList = xpath2.selectNodes(doc);
+                nodeList = xpath3.selectNodes(doc);
+                nodeList = xpath4.selectNodes(doc);
+                number  = xpath5.numberValueOf(doc);
+                number  = xpath6.numberValueOf(doc);
+
+                Node node = doc.selectSingleNode("/blog/item[@num='a781']");
+                node.detach();
+
+                nodeList = xpath7.selectNodes(doc);
+                Iterator it = nodeList.iterator();
+                while (it.hasNext()) {
+                    node = (Node) it.next();
+                    node.detach();
+                }
+
+
+                bais.reset();
+                writer.write(doc);
+                baos.reset();
+
+            }
+            end = System.currentTimeMillis();
+            if (forReal)
+                System.err.println("cycle average (ms): " + (end - start) / kk + " ms");
+
+
+        } catch (Exception ie) {
+            ie.printStackTrace();
+        }
+    }
+
+}

TransformVTD.java

+import java.io.*;
+import java.util.*;
+import com.ximpleware.*;
+
+public class TransformVTD {
+
+    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) {
+        XMLModifier xm = new XMLModifier();
+        VTDGen vg = new VTDGen();
+        VTDNav vn = null;
+        AutoPilot ap = new AutoPilot();
+        long start;
+        long end;
+        int kk=10;
+        double time = 0.0;
+        int i=0;
+        int j = 0;
+        double res = 0;
+
+
+        try {
+            byte[] content = getBytesFromFile(new File("blog.xml"));
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(content.length * 2);
+            start = System.currentTimeMillis();
+
+            for (int ii=0; ii<kk; ii++) {
+                vg.setDoc(content);
+                vg.parse(false);
+                vn = vg.getNav();
+                xm.bind(vn);
+                ap = new AutoPilot();
+                ap.selectXPath("//item");
+                ap.bind(vn);
+                while ((i=ap.evalXPath()) != -1) {
+                    j++;
+                    //do nothing, just iterate through them
+                }
+                ap.resetXPath();
+                ap.selectXPath("/blog/item");
+                ap.bind(vn);
+                while ((i=ap.evalXPath()) != -1) {
+                    j++;
+                    //do nothing, just iterate through them
+                }
+                ap.resetXPath();
+                ap.selectXPath("//text()");
+                ap.bind(vn);
+                while ((i=ap.evalXPath()) != -1) {
+                    j++;
+                    //do nothing, just iterate through them
+                }
+                ap.resetXPath();
+                ap.selectXPath("count(//item)");
+                ap.bind(vn);
+                res = ap.evalXPathToNumber();
+                ap.resetXPath();
+                ap.selectXPath("count(/blog/item)");
+                ap.bind(vn);
+                res = ap.evalXPathToNumber();
+                ap.resetXPath();
+                ap.selectXPath("/blog/item[@num='a781']");
+                ap.bind(vn);
+                while ((i=ap.evalXPath()) != -1) {
+                    j++;
+                    //xm.remove();
+                }
+                ap.resetXPath();
+                ap.selectXPath("/blog/item/body/p/a");
+                ap.bind(vn);
+                while ((i=ap.evalXPath()) != -1) {
+                    j++;
+                    xm.remove();
+                }
+
+                //xm.output(baos);
+                xm.reset();
+                //baos.reset();
+            }
+
+            end = System.currentTimeMillis();
+            time = (end - start) / kk;
+            System.err.println("average cycle time  (ms): " + time);
+
+        } catch (Exception ie) {
+            ie.printStackTrace();
+        }
+    }
+
+}