JAVA解析XML的几种方法

1. 解析方式 DOM与SAX

目前解析XML有两种方式DOM和SAX,它们的解析原理不相同。

DOM是树形结构解析,解析之前全部加载入内存,适合对XML的随机访问。由于加载到内存,所以处理大型文件时其性能下降的非常厉害。

SAX是事件驱动型的XML解析方式。它顺序读取XML文件,不需要一次全部装载整个文件。当遇到像文件开头,文档结束,或者标签开头与标签结束时,它会触发一个事件,用户通过在其回调事件中写入处理代码来处理XML文件,适合对XML的顺序访问。

2. JAVA解析XML的4种方法

其中DOM是DOM方式解析, JDOM,DOM4J,SAX是SAX方式解析。一般公司都使用DOM4J和JDOM,因为编码比较简单。

这里介绍简单,可以根据各自需要进行封装。

假设XML的格式为:
<?xml version="1.0" encoding="GB2312" standalone="no"?>
<campany name="maymay">
	<department>
		<name>Human Resource</name>
		<employees>
			<staff>Ying Liu</staff>
			<staff>Hongwei Dai</staff>
			<staff>Fang Liu</staff>
		</employees>
	</department>
</campany>


定义JAVA接口:
package xml;

public interface XmlDocument {
	

	/**
	 * 
	 * @param fileName:文件路径及名称
	 */
	public void createXml(String fileName);

	/**
	 * 
	 * @param fileName:文件路径及名称
	 */
	public void parserXml(String fileName);
}


2.1. DOM方法

所需包已经在JDK中。

package xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParse implements XmlDocument {
	private Document document;
	private String fileName;

	public DomParse() {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			this.document = builder.newDocument();
			
		} catch (ParserConfigurationException e) {
			System.out.println(e.getMessage());
		}
	}

	public void createXml(String fileName) {

		Element root = this.document.createElement("campany");
		root.setAttribute("name", "maymay");
		this.document.appendChild(root);

		Element department = this.document.createElement("department");
		root.appendChild(department);

		Element name = this.document.createElement("name");
		name.appendChild(this.document.createTextNode("Human Resource"));

		department.appendChild(name);

		Element employees = this.document.createElement("employees");
		department.appendChild(employees);

		Element staff1 = this.document.createElement("staff");
		Element staff2 = this.document.createElement("staff");
		Element staff3 = this.document.createElement("staff");
		
		staff1.appendChild(this.document.createTextNode("Ying Liu"));
		staff2.appendChild(this.document.createTextNode("Hongwei Dai"));
		staff3.appendChild(this.document.createTextNode("Fang Liu"));

		employees.appendChild(staff1);
		employees.appendChild(staff2);
		employees.appendChild(staff3);

		TransformerFactory tf = TransformerFactory.newInstance();

		try {
			Transformer transformer = tf.newTransformer();
			DOMSource source = new DOMSource(document);
			transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);

		} catch (TransformerConfigurationException e) {
			System.out.println(e.getMessage());
		} catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (TransformerException e) {
			System.out.println(e.getMessage());
		}
	}

	public void parserXml(String fileName) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(fileName);
			NodeList campanys = document.getChildNodes();
			
			for (int i = 0; i < campanys.getLength(); i++) {
				Node departments = campanys.item(i);
				
				NodeList department = departments.getChildNodes();
				
				for (int j = 0; j < department.getLength(); j++) {
					Node node = department.item(j);
					NodeList meta = node.getChildNodes();
					
					for (int k = 0; k < meta.getLength(); k++) {
						System.out.println(meta.item(k).getNodeName()
								+ ":" + meta.item(k).getTextContent());
					}
				}
			}
			
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (ParserConfigurationException e) {
			System.out.println(e.getMessage());
		} catch (SAXException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}


2.2. SAX解析

所需包已经在JDK中。
package xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParse implements XmlDocument {
	
	public void createXml(String fileName) {
		System.out.println("<<" + fileName + ">>");
	}

	public void parserXml(String fileName) {
		SAXParserFactory saxfac = SAXParserFactory.newInstance();
		try {
			SAXParser saxparser = saxfac.newSAXParser();
			InputStream is = new FileInputStream(fileName);
			saxparser.parse(is, new MySAXHandler());
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class MySAXHandler extends DefaultHandler {
	boolean hasAttribute = false;
	Attributes attributes = null;

	public void startDocument() throws SAXException {
		System.out.println("begin");
	}

	public void endDocument() throws SAXException {
		System.out.println("end");
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("campany")) {
			return;
		}
		if (attributes.getLength() > 0) {
			this.attributes = attributes;
			this.hasAttribute = true;
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (hasAttribute && (attributes != null)) {
			for (int i = 0; i < attributes.getLength(); i++) {
				System.out.println("--"+attributes.getQName(0)+":"
						+ attributes.getValue(0));
			}
		}
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		System.out.println(new String(ch, start, length));
	}
}



2.3. JDOM解析

需要jdom1.0.jar

package xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class JDomParse implements XmlDocument {

	/**
	 * 
	 */
	public void createXml(String fileName) {
		Document document;
		Element root;
		root = new Element("campany");
		root.setAttribute("name", "MayMay");
		document = new Document(root);

		Element department = new Element("department");
		root.addContent(department);

		Element name = new Element("name");
		name.setText("Human Resource");
		department.addContent(name);

		Element employees = new Element("employees");
		department.addContent(employees);

		Element staff1 = new Element("staff");
		staff1.setText("Ying Liu");
		employees.addContent(staff1);

		Element staff2 = new Element("staff");
		staff2.setText("Hongwei Dai");
		employees.addContent(staff2);

		Element staff3 = new Element("staff");
		staff3.setText("Fang Liu");
		employees.addContent(staff3);

		XMLOutputter XMLOut = new XMLOutputter();
		try {
			XMLOut.output(document, new FileOutputStream(fileName));

		} catch (FileNotFoundException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();

		}

	}

	/**
	 * 
	 */
	public void parserXml(String fileName) {
		SAXBuilder builder = new SAXBuilder(false);

		try {
			Document document = builder.build(fileName);
			Element campany = document.getRootElement();

			System.out.println("--campany.name:"
					+ campany.getAttributeValue("name"));

			Element department = campany.getChild("department");

			System.out.println("--department.name:"
					+ department.getChild("name").getText());

			List staffs = department.getChildren("employees");
			System.out.println("--staffs.size:"+staffs.size());
			
			for (int i = 0; i < staffs.size(); i++) {
				
				Element staff = (Element) staffs.get(i);
				System.out.println("--"+staff.getName() + ":"+staff.getValue());
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}



2.4. DOM4J解析

需要dom4j-1.4.jar

package xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


public class Dom4jParse implements XmlDocument {
	
	/**
	 * 
	 */
	public void createXml(String fileName) {
		Document document = DocumentHelper.createDocument();
		
		Element campany = document.addElement("campany");
		campany.setAttributeValue("name", "maymay");
		
		Element department = campany.addElement("department");
		Element name = department.addElement("name");
		name.setText("Human Resource");
		
		Element employees = department.addElement("employees");
		
		Element staff1 = employees.addElement("staff");
		staff1.setText("Ying Liu");
		
		Element staff2 = employees.addElement("staff");
		staff2.setText("Hongwei Dai");
		
		Element staff3 = employees.addElement("staff");
		staff3.setText("Fang Liu");
		
		try {
			Writer fileWriter = new FileWriter(fileName);
			XMLWriter xmlWriter = new XMLWriter(fileWriter);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public void parserXml(String fileName) {
		File inputXml = new File(fileName);
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(inputXml);
			Element campany = document.getRootElement();
			System.out.println("--campany.name:"+campany.attributeValue("name"));
			
			for (Iterator i = campany.elementIterator(); i.hasNext();) {
				Element department = (Element) i.next();
				
				for (Iterator j = department.elementIterator(); j.hasNext();) {
					Element node = (Element) j.next();
					System.out.println(node.getName() + ":" + node.getStringValue());
				}
			}
		} catch (DocumentException e) {
			System.out.println(e.getMessage());
		} catch (MalformedURLException e) {
			System.out.println(e.getMessage());
		}
	}
}


2.5. 测试
package xml;

public class XMLParseDemo {
	
	public static void main(String[] args){
		//JDomParse jdom = new JDomParse();
		//jdom.createXml("G:\\StudyEnviroment\\studyworkspace\\practice\\src\\xml\\Campany.xml");
		//jdom.parserXml("G:\\StudyEnviroment\\studyworkspace\\practice\\src\\xml\\Campany.xml");
		
		//Dom4jParse dom4j = new Dom4jParse();
		//dom4j.createXml("G:\\StudyEnviroment\\studyworkspace\\practice\\src\\xml\\Campany.xml");
		//dom4j.parserXml("G:\\StudyEnviroment\\studyworkspace\\practice\\src\\xml\\Campany.xml");
		
		//DomParse dom = new DomParse();
		//dom.createXml("G:\\StudyEnviroment\\studyworkspace\\practice\\src\\xml\\Campany.xml");
		//dom.parserXml("G:\\StudyEnviroment\\studyworkspace\\practice\\src\\xml\\Campany.xml");
		
		SaxParse sax= new SaxParse();
		sax.parserXml("G:\\StudyEnviroment\\studyworkspace\\practice\\src\\xml\\Campany.xml");
	}

}

你可能感兴趣的:(xml,dom,dom4j,jdom,sax)