解析一個XML文檔有哪些內容
解析有:dom和sax兩種
dom:把整個XML文檔放入內存,適合XML隨機訪問,占用內存資源大
sax:事件驅動型的XML解析方式,順序讀取,不用一次裝載整個文件,遇到標簽會觸發一個事件,適合對XML的順序訪問,占用內存資源稍小
Node:
XML 文檔的 documentElement 屬性是根節點。
nodeName 屬性是節點的名稱。nodeName 是只讀的
元素節點的 nodeName 與標簽名相同
屬性節點的 nodeName 是屬性的名稱
文本節點的 nodeName 永遠是 #text
文檔節點的 nodeName 永遠是 #document
nodeType 屬性是節點的類型。
元素類型 節點類型
元素 1
屬性 2
文本 3
注釋 8
文檔 9
nodeValue屬性規定節點的值。
元素節點的 nodeValue 是 undefined
文本節點的 nodeValue 是文本自身
屬性節點的 nodeValue 是屬性的值
DOM解析:
package com.briup.test3; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; //用遞歸DOM方法解析 public class DomBookTest { //獲取解析器 將其封裝 public static Document getDoc(String filename) throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(filename); } public static void main(String[] args) throws Exception { //獲取解析地址 Document doc = getDoc("src/com/briup/test3/book.xml"); //輸出頭部 System.out.println("<? version="+doc.getXmlVersion()+" encoding="+doc.getXmlEncoding()+">"); //獲取根節點 Element element = doc.getDocumentElement(); printdoc(element); } public static void printdoc(Node n){ //獲取節點類型 short type = n.getNodeType(); if (type==1) { //獲取節點內容 System.out.print("<"+n.getNodeName()+" "); //用Map集合存儲獲取的節點屬性 NamedNodeMap map = n.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Node attr =map.item(i); System.out.print(attr.getNodeName()+"="+attr.getNodeValue()); } System.out.print(">"); //獲取孩子節點 NodeList list = n.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node child = list.item(i); printdoc(child); } System.out.print("</"+n.getNodeName()+">"); } //獲取節點內容 else if(type ==3){ System.err.print(n.getNodeValue()); } } }
SAX解析:
package com.briup.test3; 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 SaxBookTest { public static void main(String[] args) throws Exception{ //獲取解析器 SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); //使用內部類解析文件 parser.parse("src/com/briup/test3/book.xml", new DefaultHandler(){ //解析開始標題文檔 public void startDocument() throws SAXException { System.out.println("<?xml version= 1.0 encoding= utf-8 ?>"); } //解析節點 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.print("<"+qName+" "); for (int i = 0; i < attributes.getLength(); i++) { System.out.print(attributes.getQName(i)+"="+attributes.getValue(i)); } System.out.print(">"); } @Override //解析結束 public void endElement(String uri, String localName, String qName) throws SAXException { System.out.print("</"+qName+">"); } @Override //解析內容 public void characters(char[] ch, int start, int length) throws SAXException { String string = new String(ch, start, length); System.out.print(string); } }); } }
BOOK的XML文檔
<?xml version="1.0" encoding="utf-8"?> <books> <book bid="1"> <name>java與模式</name> <price>80</price> </book> <book bid="2"> <name>java編程思想</name> <price>95</price> </book> <book bid="3"> <name>瘋狂java講義</name> <price>90</price> </book> </books>