Java 解析 xml 之 DOM and SAX

來源:互聯網
上載者:User

Java解析XML有兩種方式:

**DOM

**SAX

例子:

<?xml version="1.0" encoding="UTF-8"?><users><description>information of users</description><user><name>LiLei</name><sex>1</sex><age>19</age></user><user><name>Han Meimei</name><sex>0</sex><age>18</age></user></users>

DOM方式:

載入XML,在記憶體中建立與xml檔案相對應的DOM樹。

根據DOM樹中的Node和NodeList,擷取需要的資訊。

public boolean parserXml(InputStream is) throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse(is);NodeList descNodes = doc.getElementsByTagName("description");NodeList userNodes = doc.getElementsByTagName("user");if (descNodes.getLength() > 0) {// ignore following descriptionsdocument.setDescription(descNodes.item(0).getFirstChild().getNodeValue());}Node node;NodeList nodes;String name;User user;for (int i = 0; i < userNodes.getLength(); i++) {user = new User();nodes = userNodes.item(i).getChildNodes();for (int j = 0; j < nodes.getLength(); j++) {node = nodes.item(j);name = node.getNodeName();if ("name".equals(name)) {user.setName(node.getFirstChild().getNodeValue());} else if ("sex".equals(name)) {user.setSex(Integer.parseInt(node.getFirstChild().getNodeValue()));} else if ("age".equals(name)) {user.setAge(Integer.parseInt(node.getFirstChild().getNodeValue()));}}document.addUser(user);}return true;}

** getChildNodes() 中會包含空白和換行,所以 nodes.getLength() 傳回值是7,除了3個子節點,還有4個換行+tab。所以第一個childNode是 \n\t\t

** name節點的子節點有1個, 即 LiLei 這個#text節點

SAX方式:

事件驅動方式。

讀取xml檔案流,根據讀取到的節點觸發相應的事件。常用的如 startDocument()、endDocument()、startElement()、endElement()、characters()。。。。。

/** * event driven parse xml as an input stream, with a handler as a callback while * meeting each element tag startElement will contain lots of if/else *  * @author xuefeng *  */public class SaxXmlDemo implements IXmlDocument {private UserDocument document;public SaxXmlDemo() {document = new UserDocument();}public boolean parserXml(InputStream is) throws Exception {SAXParserFactory saxfac = SAXParserFactory.newInstance();SAXParser saxparser = saxfac.newSAXParser();saxparser.parse(is, new MySAXHandler(document));return true;}public static void main(String[] args) throws Exception {SaxXmlDemo demo = new SaxXmlDemo();FileInputStream fis = new FileInputStream("data/test.xml");demo.parserXml(fis);System.out.println();}}class MySAXHandler extends DefaultHandler {private UserDocument document;private User user; // userd for startElement and endElementprivate String curTag; // userd for startElement and endElementpublic MySAXHandler(UserDocument document) {this.document = document;}public void startDocument() throws SAXException {}public void endDocument() throws SAXException {}public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {curTag = qName;if ("user".equals(qName)) {user = new User();document.addUser(user);}}public void endElement(String uri, String localName, String qName)throws SAXException {curTag = null;}public void characters(char[] ch, int start, int length)throws SAXException {if (curTag == null)return;String value = new String(ch, start, length);if ("description".equals(curTag)) {document.setDescription(value);} else if ("name".equals(curTag)) {user.setName(value);} else if ("sex".equals(curTag)) {user.setSex(Integer.parseInt(value));} else if ("age".equals(curTag)) {user.setAge(Integer.parseInt(value));}}}

其中,

public void startElement(String uri, String localName, String qName, Attributes attributes)

uri : 如果解析器支援命名空間,uri為命名空間;如果不支援、或是沒有命名空間, uri=""

localName : 如果解析器支援命名空間,為tag的名稱;否則,為空白。

qName : 如果tag有命名空間首碼,為 首碼+tag名稱;否則,為tag名稱。

attributes : tag的屬性列表,可通過getLength()、getValue() 訪問。

本文代碼: svn  : http://hsuehfeng.googlecode.com/svn/trunk/xml/XMLParse/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.