四大xml解析器测试代码

DOMMain.java


package wkx;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class DOMMain {

	public static void parser() {
		long lasting = System.currentTimeMillis();
		try {

			File f = new File("C:/Users/Jack_Wong/Desktop/demo.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(f);
			
			NodeList list = doc.getElementsByTagName("node");
			for (int i = 0; i < list.getLength(); i++) {
				doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
				doc.getElementsByTagName("space").item(i).getFirstChild().getNodeValue();
				/*System.out.println("name"+i+":"
						+ doc.getElementsByTagName("name").item(i)
								.getFirstChild().getNodeValue());
				System.out.println("space"+i+":"
						+ doc.getElementsByTagName("space").item(i)
								.getFirstChild().getNodeValue());*/
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("DOM RUNTIME:"
				+ (System.currentTimeMillis() - lasting) + " MS");
	}

	public static void main(String[] args) {
		parser();
	}

}


JDOMMain.java



package wkx;

import java.io.File;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class JDOMMain {

	public static void parser() {

		long lasting = System.currentTimeMillis();
		try {
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(new File(
					"C:/Users/Jack_Wong/Desktop/demo.xml"));
			Element foo = doc.getRootElement();
			List<Element> allChildren = foo.getChildren();
			for (int i = 0; i < allChildren.size(); i++) {
				((Element) allChildren.get(i)).getChild("name").getText();
				((Element) allChildren.get(i)).getChild("space").getText();
				/*System.out.println("name"
						+ i
						+ ":"
						+ ((Element) allChildren.get(i)).getChild("name")
								.getText());
				System.out.println("space"
						+ i
						+ ":"
						+ ((Element) allChildren.get(i)).getChild("space")
								.getText());*/
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("JDOM RUNTIME:"
				+ (System.currentTimeMillis() - lasting) + " MS");
	}

	public static void main(String[] args) {

		parser();

	}

}




SAXMain.java




package wkx;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;

public class SAXMain extends DefaultHandler {

	public static void parser() {

		long lasting = System.currentTimeMillis();

		try {

			SAXParserFactory sf = SAXParserFactory.newInstance();
			SAXParser sp = sf.newSAXParser();
			SAXMain reader = new SAXMain();
			sp.parse(new InputSource("C:/Users/Jack_Wong/Desktop/demo.xml"),reader);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("SAX RUNTIME:" + (System.currentTimeMillis() - lasting) + " MS");

	}

	public static void main(String[] args) {
		parser();
	}

}



DOM4JMain.java



package wkx;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DOM4JMain {
	public static void parser() {
		long lasting = System.currentTimeMillis();
		try {
			File f = new File("C:/Users/Jack_Wong/Desktop/demo.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(f);
			Element root = doc.getRootElement();
			Element foo;
			for (Iterator i = root.elementIterator("node"); i.hasNext();) {
				foo = (Element) i.next();
				foo.elementText("name");
				foo.elementText("space");
				//System.out.println("name" + i + ":" + foo.elementText("name"));
				//System.out.println("space" + i + ":" + foo.elementText("space"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("DOM4J RUNTIME:"
				+ (System.currentTimeMillis() - lasting) + " MS");
	}

	public static void main(String[] args) {
		parser();
	}

}







你可能感兴趣的:(四大xml解析器测试代码)