public void run() {
long time1 = System.currentTimeMillis();
for (int i = 0; i < XercesSingleThreadTestForParsing.loopCount; i++) {
for (int j = 0; j < XercesSingleThreadTestForParsing.filesCount; j++) {
try {
Document doc = db.parse(new InputSource(new ByteArrayInputStream(XercesSingleThreadTestForParsing.fBytes[fileIndex])));
// NodeList nodeList= (NodeList) doc.getElementsByTagName("soap:Body");
// Element body = (Element) nodeList.item(0);
// System.out.println(org.apache.axis.utils.XMLUtils.DocumentToString(doc));
// org.apache.axis.utils.XMLUtils.DocumentToString(doc);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
long time2 = System.currentTimeMillis();
System.out.println("Total Time is : " + (time2 - time1));
}
public static void main(String argv[]) {
XercesSingleThreadTestForParsing test = new XercesSingleThreadTestForParsing(0);
test.run();
}
}
-----------------------------
And the VTD program is in the below :
import com.ximpleware.*;
import java.io.*;
public class SOAPProcessorTesting {
private static int loopCount = 10000;
public static void main(String argv[]) {
try {
// open a file and read the content into a byte array
// Modify this part of the code if the message is transmitted via HTTP
File f = new File("./soap2.xml");
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
fis.read(b);
VTDGen vg = new VTDGen();
long start = System.currentTimeMillis();
// instantiate the parser
for(int i =0 ;i < loopCount;i ++){
vg.setDoc(b);
vg.parse(true); // set namespace awareness to true
VTDNav vn = vg.getNav();
// if (vn.toElement(VTDNav.LC)) // to first child
// {
// vn.dumpContext();
// if (vn.toElement(VTDNav.FC)) // to first child
// {
// do {
// vn.dumpContext();
// long l = vn.getElementFragment();
// int len = (int) (l >> 32);
// int offset = (int) l;
// System.out.write(b, offset, len); //write the fragment out into out.txt
// System.out.println();
// } while (vn.toElement(VTDNav.NS)); // navigate next sibling
// } else
// System.out.println("Body has not child elements");
// }
}
long end = System.currentTimeMillis();
System.out.println("\nTotal time for loop " + loopCount + " = " + (end - start));
Hi ,
In the first , I am amazing about the idea like VTD, ... a XML encoding for xml parsing & traverse.
I take some times to understand the tech perspective & download the 0.8 version to try to parse XML document, like SOAP message.
However , compared with Xerces 2.6.2, I get some interesting result. Xerces is faster than VTD.
The SOAP message size is 5KB & the program loop is 10000. I using JDK1.5.0 , Hardware P4 3.2G , 2GB RAM, and just do "parsing".
The part of my Xerces program is very simple, as in the below.
package ccl.xml.parsers;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.*;
public class XercesSingleThreadTestForParsing implements Runnable {
private final static int loopCount = 10000;
private static int filesCount;
private static final String root = "./testDsig/sample";
private static byte[][] fBytes;
private static String[] files;
private int fileIndex;
javax.xml.parsers.DocumentBuilder db = null;
static {
String messageDir = root;
File dir = new File(messageDir);
if (dir.exists() && dir.canRead() && dir.isDirectory()) {
File[] sting = dir.listFiles();
files = new String[sting.length];
fBytes = new byte[sting.length][];
try {
for (int i = 0; i < sting.length; i++) {
files[i] = sting[i].getAbsolutePath();
System.out.println("fileIndex = "+i + " : " + files[i]);
FileInputStream ins = new FileInputStream(files[i]);
ByteArrayOutputStream ous = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int size = 0;
while ((size = ins.read(buffer)) != -1) {
ous.write(buffer, 0, size);
}
XercesSingleThreadTestForParsing.fBytes[i] = ous.toByteArray();
ins.close();
}
filesCount = files.length;
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} // end of static Initializer
public XercesSingleThreadTestForParsing(int fileIndex) {
this.fileIndex = fileIndex;
try {
javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setAttribute("http://xml.org/sax/features/namespaces";, Boolean.TRUE);
db = dbf.newDocumentBuilder();
} catch (Exception e) {
}
}
public void run() {
long time1 = System.currentTimeMillis();
for (int i = 0; i < XercesSingleThreadTestForParsing.loopCount; i++) {
for (int j = 0; j < XercesSingleThreadTestForParsing.filesCount; j++) {
try {
Document doc = db.parse(new InputSource(new ByteArrayInputStream(XercesSingleThreadTestForParsing.fBytes[fileIndex])));
// NodeList nodeList= (NodeList) doc.getElementsByTagName("soap:Body");
// Element body = (Element) nodeList.item(0);
// System.out.println(org.apache.axis.utils.XMLUtils.DocumentToString(doc));
// org.apache.axis.utils.XMLUtils.DocumentToString(doc);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
long time2 = System.currentTimeMillis();
System.out.println("Total Time is : " + (time2 - time1));
}
public static void main(String argv[]) {
XercesSingleThreadTestForParsing test = new XercesSingleThreadTestForParsing(0);
test.run();
}
}
-----------------------------
And the VTD program is in the below :
import com.ximpleware.*;
import java.io.*;
public class SOAPProcessorTesting {
private static int loopCount = 10000;
public static void main(String argv[]) {
try {
// open a file and read the content into a byte array
// Modify this part of the code if the message is transmitted via HTTP
File f = new File("./soap2.xml");
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
fis.read(b);
VTDGen vg = new VTDGen();
long start = System.currentTimeMillis();
// instantiate the parser
for(int i =0 ;i < loopCount;i ++){
vg.setDoc(b);
vg.parse(true); // set namespace awareness to true
VTDNav vn = vg.getNav();
// if (vn.toElement(VTDNav.LC)) // to first child
// {
// vn.dumpContext();
// if (vn.toElement(VTDNav.FC)) // to first child
// {
// do {
// vn.dumpContext();
// long l = vn.getElementFragment();
// int len = (int) (l >> 32);
// int offset = (int) l;
// System.out.write(b, offset, len); //write the fragment out into out.txt
// System.out.println();
// } while (vn.toElement(VTDNav.NS)); // navigate next sibling
// } else
// System.out.println("Body has not child elements");
// }
}
long end = System.currentTimeMillis();
System.out.println("\nTotal time for loop " + loopCount + " = " + (end - start));
fis.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
}
Sorry for the late reply, I suggest that
1. download the latest release of VTD-XML which is 1.0
2. make sure you use the SERVER JVM