[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;  
    }  
}

运行结果如图所示:

 

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