原文章地址:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/17/3082949.html
首先给当初四种方法的jar包下载地址:
DOM:在J2SE标准包中自带DOM
SAX:http://sourceforge.net/projects/sax/
JDOM:http://jdom.org/downloads/index.html
DOM4J:http://sourceforge.net/projects/dom4j/
<?xml version="1.0" encoding="UTF-8"?> <users> <user id="0"> <name>Alexia</name> <age>23</age> <sex>Female</sex> </user> <user id="1"> <name>Edward</name> <age>24</age> <sex>Male</sex> </user> <user id="2"> <name>wjm</name> <age>23</age> <sex>Female</sex> </user> <user id="3"> <name>wh</name> <age>24</age> <sex>Male</sex> </user> </users>
package com.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; /** * @author Alexia * * DOM 解析XML文档 */ public class DomDemo implements XmlDocument { private Document document; public void parserXml(String fileName) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(fileName); NodeList users = document.getChildNodes(); for (int i = 0; i < users.getLength(); i++) { Node user = users.item(i); NodeList userInfo = user.getChildNodes(); for (int j = 0; j < userInfo.getLength(); j++) { Node node = userInfo.item(j); NodeList userMeta = node.getChildNodes(); for (int k = 0; k < userMeta.getLength(); k++) { if(userMeta.item(k).getNodeName() != "#text") System.out.println(userMeta.item(k).getNodeName() + ":" + userMeta.item(k).getTextContent()); } System.out.println(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package com.xml; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.StringWriter; 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.SAXException; import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.DefaultHandler; /** * @author Alexia * * SAX 解析XML文档 */ public class SaxDemo implements XmlDocument { 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("文档开始打印了"); } public void endDocument() throws SAXException { // System.out.println("文档打印结束了"); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equals("users")) { return; } if (qName.equals("user")) { 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.print(attributes.getQName(0) + ":" + attributes.getValue(0)); } } } public void characters(char[] ch, int start, int length) throws SAXException { System.out.print(new String(ch, start, length)); } }
package com.xml; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.XMLOutputter; /** * @author Alexia * * JDOM 解析XML文档 * */ public class JDomDemo implements XmlDocument { public void parserXml(String fileName) { SAXBuilder builder = new SAXBuilder(); try { Document document = builder.build(fileName); Element users = document.getRootElement(); List userList = users.getChildren("user"); for (int i = 0; i < userList.size(); i++) { Element user = (Element) userList.get(i); List userInfo = user.getChildren(); for (int j = 0; j < userInfo.size(); j++) { System.out.println(((Element) userInfo.get(j)).getName() + ":" + ((Element) userInfo.get(j)).getValue()); } System.out.println(); } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package com.xml; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; 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; /** * @author Alexia * * Dom4j 解析XML文档 */ public class Dom4jDemo implements XmlDocument { public void parserXml(String fileName) { File inputXml = new File(fileName); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(inputXml); Element users = document.getRootElement(); for (Iterator i = users.elementIterator(); i.hasNext();) { Element user = (Element) i.next(); for (Iterator j = user.elementIterator(); j.hasNext();) { Element node = (Element) j.next(); System.out.println(node.getName() + ":" + node.getText()); } System.out.println(); } } catch (DocumentException e) { System.out.println(e.getMessage()); } } }