[java开发篇][dom模块] 遍历解析xml

http://blog.csdn.net/andie_guo/article/details/24844351

XML DOM节点树

XML DOM将XML文档作为树结构,树结构称为一个节点树。所有的节点可以通过树访问,它们的内容可以被修改或删除,也可以建立新的元素。节点树用于显示节点集和它们之间的联系。下图呈现的是books.XML文件的节点树。

[java开发篇][dom模块] 遍历解析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;
}
}

运行结果如图所示:

[java开发篇][dom模块] 遍历解析xml

  1. package com.andieguo.xmldemo;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.Node;
  10. import org.w3c.dom.NodeList;
  11. public class ReadXMLFile {
  12. public static void main(String[] args) {
  13. File file = new File("src/com/andieguo/xmldemo/books.xml");//books.xml文件应放在和ReadXMLFile.java同级的文件夹下
  14. List<Book> books = readXMLFile(file);
  15. for (Book book : books) {
  16. System.out.println(book.toString());
  17. }
  18. }
  19. public static List<Book> readXMLFile(File file) {
  20. List<Book> lists = new ArrayList<Book>();
  21. try {
  22. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  23. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  24. Document doc = dBuilder.parse(file);
  25. NodeList bookList = doc.getElementsByTagName("book");
  26. for (int i = 0; i < bookList.getLength(); i++) {
  27. Node bookNode = bookList.item(i);
  28. if (bookNode.getNodeType() == Node.ELEMENT_NODE) {
  29. Element bookElement = (Element) bookNode;
  30. Book book = new Book();
  31. book.setCategory(bookElement.getAttribute("category"));
  32. Element titleElement = (Element) bookElement.getElementsByTagName("title").item(0);
  33. book.setTitle(titleElement.getTextContent());
  34. book.setTitleLang(titleElement.getAttribute("lang"));
  35. NodeList authorList = bookElement.getElementsByTagName("author");
  36. String author = "";
  37. for (int j = 0; j < authorList.getLength(); j++) {
  38. author = author + authorList.item(j).getTextContent() + "/";
  39. }
  40. author = author.substring(0, author.length() - 1);
  41. book.setAuthor(author);
  42. book.setYear(Integer.valueOf(bookElement.getElementsByTagName("year").item(0).getTextContent()));
  43. book.setPrice(Double.valueOf(bookElement.getElementsByTagName("price").item(0).getTextContent()));
  44. lists.add(book);
  45. }
  46. }
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. return lists;
  51. }
  52. }
上一篇:【Java】Jackson解析xml的坑


下一篇:实例快速上手 -ASP.NET 4.5新特性WebAPI从入门到精通