解析XML有两种(不限于)方法: 一种是基于DOM(Document Object Model)的解析,一种是基于事件(event-based)的SAX(Sample API for XML)解析。
DOM
-Tree model parser(Object based) (Tree of nodes).
-DOM 加载所有XML到内存,然后解析。因此具有内存限制。
-DOM 可以对XML进行读写操作。
-DOM 适宜于小型的XML。
-可以对节点进行前后检索。
-运行速度慢
-JDK 7 内置
SAX
-Event based parser (Sequence of events).
-SAX 按节点,一边读一边解析。因此没有内存限制。
-SAX 只能对XML进行读操作。
-SAX 可以解析大型的XML。
-SAX 从文件头读到文件尾,不可向后检索。
-运行速度快
-需要引用第三方包:jdom
示例:
XML(text.xml)
dinkar kad dinkar 85 Vaneet Gupta vinni 95 jasvir singn jazz 90
DOM 解析
package com.gentleman.util.xml; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMParser { public static void main(String[] args) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse( DOMParser.class.getClassLoader().getResourceAsStream("test.xml")); document.getDocumentElement().normalize(); System.out.println("Root Element : " + document.getDocumentElement().getNodeName()); System.out.println("----------------------------"); NodeList nList = document.getElementsByTagName("student"); for (int temp = 0; temp < nList.getLength(); temp++) { Node node = nList.item(temp); Element element = (Element) node; System.out.println(element.getNodeName() + " : " + element.getAttribute("rollno")); System.out.println("First Name : " + getText(element, "firstname")); System.out.println("Last Name : " + getText(element, "lastname")); System.out.println("Nick Name : " + getText(element, "nickname")); System.out.println("Marks : " + getText(element, "marks")); System.out.println("\n"); } }//end main(). private static String getText(Element element, String tag){ return element .getElementsByTagName(tag) .item(0) // get the first element of the tag name. .getTextContent(); } }
SAX解析
package com.gentleman.util.xml; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class SAXParser { public static void main(String[] args) throws Exception { SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(SAXParser.class.getClassLoader() .getResourceAsStream("test.xml")); Element root = doc.getRootElement(); List list = root.getChildren("student"); for (int i = 0; i < list.size(); i++) { Element element = (Element) list.get(i); String rollno = element.getAttributeValue("rollno"); String firstname = element.getChildText("firstname"); String lastname = element.getChildTextTrim("lastname"); String nickname = element.getChild("nickname").getText(); String marks = element.getChild("marks").getTextTrim(); System.out.println("Student: " + rollno); System.out.println("---------------------------"); System.out.println("firstname: " + firstname); System.out.println("lastname : " + lastname); System.out.println("nickname : " + nickname); System.out.println("marks : " + marks); System.out.println("\n"); } } }
引用:
http://stackoverflow.com/a/19154095/2893073
https://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm#BGBGAJIA
https://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html
-
转载请注明
原文出处:http://lixh1986.iteye.com/blog/2328760
-
-