[java开发篇][dom模块] 遍历解析xml
http://blog.csdn.net/andie_guo/article/details/24844351
XML DOM节点树
XML DOM将XML文档作为树结构,树结构称为一个节点树。所有的节点可以通过树访问,它们的内容可以被修改或删除,也可以建立新的元素。节点树用于显示节点集和它们之间的联系。下图呈现的是books.XML文件的节点树。
常用的几个对象:
1)Element类:
是Node类最主要的子对象,被广泛使用,在元素中可以包含属性,因而Element中有存取其属性的方法。
2)Node类:
Node对象是DOM中最基本的对象,代表了文档树中的抽象节点。但在实际使用中很少会直接使用Node对象,而是使用Node对象的子对象Element,Attr,Text等。
3)NodeList类:
代表了一个包含一个或者多个Node的列表,根据操作可以将其简化的看做为数组。
DOM XML Parser 解析XML文件
<?xml version="1.0" encoding="UTF-8"?> <!-- Edited by XMLSpy --> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Book.java:该对象是一个实体Bean,其字段信息对应着xml文件里的元素字段,由于篇幅有限,读者自行生成get、set方法 。
package com.andieguo.xmldemo; public class Book { private String category; private String titleLang; private String title; private String author; private Integer year; private Double price; @Override public String toString() { return "Book [category=" + category + ", titleLang=" + titleLang + ", title=" + title + ", author=" + author + ", year=" + year + ", price=" + price + "]"; } //生成字段的get、set方法 }
ReadXMLFile.java :解析XML文件并存入List<Book>集合。
package com.andieguo.xmldemo; import java.io.File; import java.util.ArrayList; import java.util.List; 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 ReadXMLFile { public static void main(String[] args) { File file = new File("src/com/andieguo/xmldemo/books.xml");//books.xml文件应放在和ReadXMLFile.java同级的文件夹下 List<Book> books = readXMLFile(file); for (Book book : books) { System.out.println(book.toString()); } } public static List<Book> readXMLFile(File file) { List<Book> lists = new ArrayList<Book>(); try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); NodeList bookList = doc.getElementsByTagName("book"); for (int i = 0; i < bookList.getLength(); i++) { Node bookNode = bookList.item(i); if (bookNode.getNodeType() == Node.ELEMENT_NODE) { Element bookElement = (Element) bookNode; Book book = new Book(); book.setCategory(bookElement.getAttribute("category")); Element titleElement = (Element) bookElement.getElementsByTagName("title").item(0); book.setTitle(titleElement.getTextContent()); book.setTitleLang(titleElement.getAttribute("lang")); NodeList authorList = bookElement.getElementsByTagName("author"); String author = ""; for (int j = 0; j < authorList.getLength(); j++) { author = author + authorList.item(j).getTextContent() + "/"; } author = author.substring(0, author.length() - 1); book.setAuthor(author); book.setYear(Integer.valueOf(bookElement.getElementsByTagName("year").item(0).getTextContent())); book.setPrice(Double.valueOf(bookElement.getElementsByTagName("price").item(0).getTextContent())); lists.add(book); } } } catch (Exception e) { e.printStackTrace(); } return lists; } }
运行结果如图所示:
- package com.andieguo.xmldemo;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- 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 ReadXMLFile {
- public static void main(String[] args) {
- File file = new File("src/com/andieguo/xmldemo/books.xml");//books.xml文件应放在和ReadXMLFile.java同级的文件夹下
- List<Book> books = readXMLFile(file);
- for (Book book : books) {
- System.out.println(book.toString());
- }
- }
- public static List<Book> readXMLFile(File file) {
- List<Book> lists = new ArrayList<Book>();
- try {
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
- Document doc = dBuilder.parse(file);
- NodeList bookList = doc.getElementsByTagName("book");
- for (int i = 0; i < bookList.getLength(); i++) {
- Node bookNode = bookList.item(i);
- if (bookNode.getNodeType() == Node.ELEMENT_NODE) {
- Element bookElement = (Element) bookNode;
- Book book = new Book();
- book.setCategory(bookElement.getAttribute("category"));
- Element titleElement = (Element) bookElement.getElementsByTagName("title").item(0);
- book.setTitle(titleElement.getTextContent());
- book.setTitleLang(titleElement.getAttribute("lang"));
- NodeList authorList = bookElement.getElementsByTagName("author");
- String author = "";
- for (int j = 0; j < authorList.getLength(); j++) {
- author = author + authorList.item(j).getTextContent() + "/";
- }
- author = author.substring(0, author.length() - 1);
- book.setAuthor(author);
- book.setYear(Integer.valueOf(bookElement.getElementsByTagName("year").item(0).getTextContent()));
- book.setPrice(Double.valueOf(bookElement.getElementsByTagName("price").item(0).getTextContent()));
- lists.add(book);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return lists;
- }
- }