闲来无事,看如何用DOM解析XML

//在下纯给自己做笔记,抱歉。



    
        aa冰与火之歌
        乔治马丁
        2014
        89
    
    
        安徒生童话
        2004
        77
        English
    


    
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package domtest;

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Administrator
 */
public class DomTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        // TODO code application logic here
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document document=db.parse("newXMLDocument.xml");
        //获取所有book节点的集合
        NodeList bookList=document.getElementsByTagName("book");
        System.out.println(bookList.getLength());
        //遍历每一个book节点
        for(int i=0;i


你可能感兴趣的:(JAVA,XML,DOM)