XML文档最基本的解析方式有两种,分别是文档对象模型(Document Object Model简称DOM)和XML解析的简单API(SAX)。
DOM解析是一种基于对象的API,它把XML的内容加载到内存中,生成一个与XML文档内容相对应的对象模型。当解析完成时,内存中会生成XML文档的结构对应的DOM对象树。这样便能够根据树的结构,以节点的形式来对文档进行操作。解析XML文件要比解析字符串、解析Properties文件复杂得多。很显然DOM解析方式由于全部在内存,所以便于操作,但是由于整个文档全部在内存中,因此如果文档很大的时候便会浪费时间和空间。因此出现SAX解析方式,SAX解析基于事件驱动,当解析器发现元素开始、结束、文本、文档的开始或结束等时,发送事件,开发人员自己编写响应这些事件的代码,保存数据。因此这个方式边读入边解析,占用资源少,内存压力小,但不适合CRUD。
下面是本人实现的四种xml解析,都是些简单小程序,希望下次在使用时能有快速找到思路。
1.使用DOM方式解析:
package com.xml.dom;
import java.io.FileOutputStream; import java.io.IOException; 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.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 DomDemo { public static void createXml(String fileName) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement("students"); // create the // root element. document.appendChild(root); Element student = document.createElement("student"); Element name = document.createElement("name"); Node alice = document.createTextNode("alice"); name.appendChild(alice); student.appendChild(name); Element gender = document.createElement("gender"); Node female = document.createTextNode("female"); gender.appendChild(female); student.appendChild(gender); Element email = document.createElement("email"); Node emailNode = document.createTextNode("123@gmail.com"); email.appendChild(emailNode); student.appendChild(email); root.appendChild(student); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult( new FileOutputStream( fileName)); transformer.transform(source, result); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void parseXml() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); Element root = document.getDocumentElement(); // get the root // element NodeList childNodes = root.getChildNodes(); for ( int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if ("书".equals(node.getNodeName())) { // System.out.println(node.getBaseURI()); NodeList childNodess = node.getChildNodes(); for ( int j = 0; j < childNodess.getLength(); j++) { Node nodei = childNodess.item(j); System.out.println(nodei.getTextContent()); // System.out.println(nodei.getNodeName() + "=" + // nodei.getNodeValue()); } } } // System.out.println(root.getTagName()); // System.out.println(root.getNodeName()); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String []args){ // DomDemo.createXml("src/student.xml"); DomDemo.parseXml(); }
}
2.使用SAX方式解析:
package com.xml.sax; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.DefaultHandler; public class SaxDemo { public static void main(String[] args) { // SaxDemo.createXml(); SaxDemo.parseXml(); } public static void createXml() { SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory .newInstance(); TransformerHandler handler; try { handler = factory.newTransformerHandler(); Transformer transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream out = new FileOutputStream("src/computers.xml"); Result result = new StreamResult(out); handler.setResult(result); handler.startDocument(); AttributesImpl attr = new AttributesImpl(); handler.startElement("", "", "computers", attr); attr.clear(); attr.addAttribute("", "name", "name", "", "dell"); attr.addAttribute("", "country", "country", "", "US"); handler.startElement("", "", "dell", attr); handler.characters("dell's computer is cheap!".toCharArray(), 0, "dell's computer is cheap!".length()); handler.endElement("", "", "dell"); attr.clear(); attr.addAttribute("", "name", "name", "", "levono"); attr.addAttribute("", "country", "country", "", "CN"); handler.startElement("", "", "lenovo", attr); handler.characters("lenovo's computer is good!".toCharArray(), 0, "lenovo's computer is good!".length()); handler.endElement("", "", "lenovo"); handler.endElement("", "", "computers"); handler.endDocument(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void parseXml() { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser sp; try { sp = factory.newSAXParser(); XMLReader reader = sp.getXMLReader(); reader.setContentHandler( new MySaxHandler()); reader.parse("src/book.xml"); // sp.parse(new File("src/book.xml"), new MySaxHandler()); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MySaxHandler extends DefaultHandler { public void characters( char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub System.out.println( new String(ch, start, length)); } public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub System.out.println("</" + qName + ">"); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub System.out.println("<" + qName + ">"); for ( int i = 0; attributes != null && i < attributes.getLength(); i++) { String attName = attributes.getQName(i); String attValue = attributes.getValue(i); System.out.println(attName + "=" + attValue); } }
}
3.使用Dom4j方式解析:
package com.xml.dom4j; import java.io.File; import java.io.FileWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Iterator; 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 Dom4jDemo { public static void createXml(String fileName) { Document document = DocumentHelper.createDocument(); Element employees = document.addElement("employees"); Element employee = employees.addElement("employee"); Element name = employee.addElement("name"); name.setText("jobs" ); Element age = employee.addElement("age"); age.setText("24"); Element employee1 = employees.addElement("employee"); Element name1 = employee1.addElement("name"); name1.setText("steven"); Element age1 = employee1.addElement("age"); age1.setText("39"); try { Writer fileWriter = new FileWriter(fileName); XMLWriter xmlWriter = new XMLWriter(fileWriter); xmlWriter.write(document); xmlWriter.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void parseXml() { SAXReader reader = new SAXReader(); try { Document document = reader.read( new File("src/book.xml")); // Document document = DocumentHelper.parseText() Element root = document.getRootElement(); // System.out.println(root.getName()); Iterator it = root.elementIterator(); while (it.hasNext()) { Element e = (Element) it.next(); for(Iterator i=e.elementIterator(); i.hasNext();){ Element node = (Element) i.next(); System.out.println(node.getText()); } } System.out.println("the file ends!"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // Dom4jDemo.createXml("src/employee.xml"); Dom4jDemo.parseXml(); }
}
4.使用Jdom方式解析:
View Code
package com.xml.jdom; import java.io.File; 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 JdomDemo { public static void createXml() { try { Element root = new Element("authors"); Document document = new Document(root); Element author1 = new Element("author"); Element name1 = new Element("name"); name1.setText("turing"); Element contribution1 = new Element("contribution"); contribution1.setText("Turing Machine"); Element author2 = new Element("author"); Element name2 = new Element("name"); name2.setText("Von Neumann"); Element contribution2 = new Element("contribution"); contribution2.setText("ENIAC"); author1.addContent(name1); author1.addContent(contribution1); author2.addContent(name2); author2.addContent(contribution2); root.addContent(author1); root.addContent(author2); document.setRootElement(root); XMLOutputter out = new XMLOutputter(); out.output(document, new FileOutputStream("src/authors.xml")); } catch (Exception e) { e.printStackTrace(); } } public static void parseXml() { SAXBuilder builder = new SAXBuilder(); try { Document document = builder.build( new File("src/book.xml")); Element root = document.getRootElement(); List<Element> list = root.getChildren(); for (Element element : list) { System.out.println("<书名>" + element.getChildText("书名") + "<书名/>"); System.out.println("<作者>" + element.getChildText("作者") + "<作者/>"); System.out.println("<售价>" + element.getChildText("售价") + "<售价/>"); } } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // JdomDemo.createXml(); JdomDemo.parseXml(); }