Dom解析是將xml文件全部載入,組裝成一顆dom樹,然后通過節(jié)點(diǎn)以及節(jié)點(diǎn)之間的關(guān)系來(lái)解析xml文件,下面結(jié)合這個(gè)xml文件來(lái)進(jìn)行dom解析。
- <?xml version="1.0" encoding="UTF-8"?>
- <books>
- <book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book>
- </books>
然后結(jié)合一張圖來(lái)發(fā)現(xiàn)dom解析時(shí)需要注意的地方
在這里當(dāng)我們得到節(jié)點(diǎn)book時(shí),也就是圖中1所畫的地方,如果我們調(diào)用它的getChildNodes()方法,大家猜猜它的子節(jié)點(diǎn)有幾個(gè)?不包括它的孫子節(jié)點(diǎn),thinking in java這種的除外,因?yàn)樗菍O子節(jié)點(diǎn)。它總共有5個(gè)子節(jié)點(diǎn),分別是圖中2、3、4、5、6所示的那樣。所以在解析時(shí),一定要小心,不要忽略空白的地方。
然后看代碼來(lái)解析book.xml文件
DomParseService.java
- import java.io.InputStream;
- 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.NodeList;
- import org.w3c.dom.Node;
-
- import com.xtlh.cn.entity.Book;
-
- public class DomParseService {
- public List<Book> getBooks(InputStream inputStream) throws Exception{
- List<Book> list = new ArrayList<Book>();
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(inputStream);
- Element element = document.getDocumentElement();
-
- NodeList bookNodes = element.getElementsByTagName("book");
- for(int i=0;i<bookNodes.getLength();i++){
- Element bookElement = (Element) bookNodes.item(i);
- Book book = new Book();
- book.setId(Integer.parseInt(bookElement.getAttribute("id")));
- NodeList childNodes = bookElement.getChildNodes();
-
- for(int j=0;j<childNodes.getLength();j++){
- if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){
- if("name".equals(childNodes.item(j).getNodeName())){
- book.setName(childNodes.item(j).getFirstChild().getNodeValue());
- }else if("price".equals(childNodes.item(j).getNodeName())){
- book.setPrice(Float.parseFloat(childNodes.item(j).getFirstChild().getNodeValue()));
- }
- }
- }
- list.add(book);
- }
- return list;
- }
- }
Book.java用來(lái)組裝數(shù)據(jù)和盛放數(shù)據(jù)
- public class Book {
- private int id;
- private String name;
- private float price;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- @Override
- public String toString(){
- return this.id+":"+this.name+":"+this.price;
- }
- }
測(cè)試使用單元測(cè)試如下ParseTest.java
- public class ParseTest extends TestCase{
-
- public void testDom() throws Exception{
- InputStream input = this.getClass().getClassLoader().getResourceAsStream("book.xml");
- DomParseService dom = new DomParseService();
- List<Book> books = dom.getBooks(input);
- for(Book book : books){
- System.out.println(book.toString());
- }
- }
- }
原打算將dom解析和Sax解析寫在一起的,沒想到超過字?jǐn)?shù)了,只能分開寫了,如果對(duì)Sax解析感興趣,可以到下面的鏈接去看:http://sinye.iteye.com/blog/763895
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。