1.使用DOM方式解析:
1 package com.wzh.dom; 3 import java.util.Iterator; 5 import javax.xml.parsers.DocumentBuilder; 6 import javax.xml.parsers.DocumentBuilderFactory; 8 import org.w3c.dom.Document; 9 import org.w3c.dom.Element; 10 import org.w3c.dom.NamedNodeMap; 11 import org.w3c.dom.Node; 12 import org.w3c.dom.NodeList; 14 public class DomHandler { 15 16 /* 17 * 解析XML 18 */ 19 public void read(String fileName) throws Exception { 20 // 定義工廠API 使應用程序能夠從XML文檔獲取生成DOM對象樹的解析器 21 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 22 // 獲取此類的實例之后,將可以從各種輸入源解析XML 23 DocumentBuilder builder = factory.newDocumentBuilder(); 24 // builder.parse(this.getClass().getResourceAsStream("/" + fileName)); 25 // Document接口表示整個HTML或XML文檔,從概念上講,它是文檔樹的根,並提供對文檔數據的基本訪問 26 Document document = builder.parse(this.getClass().getResourceAsStream( 27 "/" + fileName)); 28 // 獲取根節點 29 Element root = document.getDocumentElement(); 30 System.out.println(root.getNodeName()); 31 32 //讀取database節點NodeList接口提供對節點的有序集合的抽象 33 NodeList nodeList = root.getElementsByTagName("database"); 34 for (int i = 0; i < nodeList.getLength(); i++) { 35 // 獲取一個節點 36 Node node = nodeList.item(i); 37 // 獲取該節點所有屬性 38 NamedNodeMap attributes = node.getAttributes(); 39 for (int j = 0; j < attributes.getLength(); j++) { 40 Node attribute = attributes.item(j); 41 System.out.println(attribute.getNodeName() + ":" 42 + attribute.getNodeValue()); 43 } 44 //獲取所有子節點數據 45 NodeList childNodes=node.getChildNodes(); 46 for (int j = 0; j < childNodes.getLength(); j++) { 47 Node childNode=childNodes.item(j); 48 System.out.println(childNode.getNodeName()+":"+childNode.getNodeValue()); 49 } 50 } 51 } 52 53 public static void main(String[] args) throws Exception { 54 new DomHandler().read("data-source.xml"); 55 56 } 57 }
2.SAX方式解析:
1 package com.wzh.sax; 3 import org.xml.sax.Attributes; 4 import org.xml.sax.SAXException; 5 import org.xml.sax.helpers.DefaultHandler; 7 // 8 public class Saxhandler extends DefaultHandler { 9 10 @Override 11 public void startDocument() throws SAXException { 12 System.out.println("開始解析XML文檔..."); 13 } 14 15 @Override 16 public void endDocument() throws SAXException { 17 System.out.println("結束解析XML文檔..."); 18 } 19 20 @Override 21 public void startElement(String uri, String localName, String qName, 22 Attributes attributes) throws SAXException { 23 // TODO Auto-generated method stub 24 super.startElement(uri, localName, qName, attributes); 25 System.out.println("開始解析節點["+qName+"]..."); 26 System.out.println("共有["+attributes.getLength()+"]個屬性"); 27 } 28 29 @Override 30 public void characters(char[] ch, int start, int length) 31 throws SAXException { 32 // TODO Auto-generated method stub 33 super.characters(ch, start, length); 34 String content =new String(ch,start,length); 35 System.out.println(content); 36 } 37 38 @Override 39 public void endElement(String uri, String localName, String qName) 40 throws SAXException { 41 // TODO Auto-generated method stub 42 super.endElement(uri, localName, qName); 43 System.out.println("結束解析XML節點..."); 44 } 45 }
3.DOM4J方式解析:
1 package com.wzh.dom4j; 3 import java.io.FileOutputStream; 4 import java.sql.DatabaseMetaData; 5 import java.util.Iterator; 6 import java.util.List; 8 import org.dom4j.*; 9 import org.dom4j.io.OutputFormat; 10 import org.dom4j.io.SAXReader; 11 import org.dom4j.io.XMLWriter; 12 13 public class Dom4jHandler { 15 public void add() throws Exception { 16 // 1.創建一個Document 17 Document document = DocumentHelper.createDocument(); 18 // 2.給Document添加數據 19 Element root = document.addElement("DataSource"); 20 // 添加注釋 21 root.addComment("這是注釋信息"); 22 // 在root根節點下面添加一個子節點 23 Element database = root.addElement("database"); 24 database.addAttribute("name", "mysql"); 25 database.addAttribute("version", "5.0"); 26 // 添加子節點 27 database.addElement("driver").setText("com.mysql.jdbc.Driver"); 28 database.addElement("url") 29 .setText("jdbc:mysql://localhost:3306/myjdbc"); 30 database.addElement("user").setText("root"); 31 database.addElement("password").setText("root"); 32 // 3.將Document寫出文件 33 OutputFormat format = OutputFormat.createPrettyPrint(); 34 format.setEncoding("utf-8"); 35 // FileOutputStream默認生成的路徑在根路徑 36 XMLWriter xw = new XMLWriter(new FileOutputStream("db.xml"), format); 37 xw.write(document); 38 xw.close(); 39 } 40 41 public void update(String fileName) throws Exception { 42 // sax解析器 43 SAXReader saxReader = new SAXReader(); 44 // 讀到對象 45 Document document = saxReader.read(this.getClass().getResourceAsStream( 46 "/" + fileName)); 47 Element root = document.getRootElement(); 48 List<Element> databases_node = root.elements("database"); 49 for (Element database_node : databases_node) { 50 if (database_node.attributeValue("name").equalsIgnoreCase("mysql")) { 51 System.out.println("old:" 52 + database_node.attributeValue("name")); 53 database_node.attribute("name").setText("Oracle"); 54 System.out.println("update:" 55 + database_node.attributeValue("name")); 56 57 database_node.element("driver").setText("oracel"); 58 database_node.element("url").setText("jdbc"); 59 60 // 刪除password節點 61 database_node.remove(database_node.element("password")); 62 63 // 刪除屬性 64 database_node.remove(database_node.attribute("version")); 65 } 66 } 67 68 OutputFormat format = OutputFormat.createPrettyPrint(); 69 format.setEncoding("utf-8"); 70 // FileOutputStream默認生成的路徑在根路徑 71 XMLWriter xw = new XMLWriter(new FileOutputStream("db2.xml"), format); 72 xw.write(document); 73 xw.close(); 74 } 75 76 public void read(String fileName) throws Exception { 77 // sax解析器 78 SAXReader saxReader = new SAXReader(); 79 // 讀到對象 80 Document document = saxReader.read(this.getClass().getResourceAsStream( 81 "/" + fileName)); 82 Element root = document.getRootElement(); 83 System.out.println("根節點:" + root.getName()); 84 85 // List<Element> childElements=root.elements(); 86 List<Element> childElements = root.elements("database"); 87 for (Element child : childElements) { 88 // 獲取屬性 不知道屬性名稱時的遍歷方法 89 List<Attribute> attributes = child.attributes(); 90 // for (Attribute attribute : attributes) { 91 // System.out.println(attribute.getName()+":"+attribute.getValue()); 92 // } 93 String name = child.attributeValue("name"); 94 // String version = child.attributeValue("version"); 95 String version = child.attribute("version").getValue(); 96 System.out.println(name + ":" + version); 97 98 // //獲取子節點 99 // List<Element> childs=child.elements(); 100 // for (Element element : childs) { 101 // System.out.println(element.getName()+":"+element.getText()); 102 // } 103 System.out.println(child.elementText("driver")); 104 System.out.println(child.element("url").getText()); 105 System.out.println(child.elementTextTrim("user")); 106 System.out.println(child.element("password").getTextTrim()); 107 108 } 109 } 110 111 public static void main(String[] args) throws Exception { 112 // new Dom4jHandler().read("data-source.xml"); 113 // new Dom4jHandler().add(); 114 new Dom4jHandler().update("data-source.xml"); 115 } 116 }
4.總結:
DOM:在解析文件之前需要將文檔一次性加載到內存中,適合對文件的隨機訪問,不適合順序訪問。
SAX:是基於事件驅動的解析方式,它順序讀取XML文件,當遇到文檔開始,文檔結束,標簽開始,標簽結束時都會觸發響應的事件,用戶通過在其回調事件中寫入處理代碼,適合對XML的順序訪問。
DOM4j:dom4j是一個簡單的開源庫,用於處理XML、 XPath和XSLT,它基於Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP,是目前比較流行XML文檔解析方法。