大鸟的学习乐园
路漫漫其修远兮,吾将上下而求索
posts - 26,comments - 27,trackbacks - 0
package demo.xml;

//作者 sunchengjun
//时间 2006年11日5日
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMParsePage {

public DOMParsePage() {
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
try {
     DocumentBuilder dombuilder=domfac.newDocumentBuilder();
InputStream is=new FileInputStream(
"C:/123.xml");
Document doc=dombuilder.parse(is);
Element root=doc.getDocumentElement();
NodeList books=root.getChildNodes();
if(books!=null){
for(int i=0;i<books.getLength();i++){
Node book=books.item(i);
     for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling())
             {
if(node.getNodeType()==Node.ELEMENT_NODE){  

if(node.getNodeName().equals(
"title")){
     String bookname=node.getFirstChild().getNodeValue();
     System.out.println(bookname);
        }

         if(node.getNodeName().equals(
"author")){
     String author1=node.getFirstChild().getNodeValue();
     System.out.println(author1);
               }
         
  if(node.getNodeName().equals(
"description")){
        String addtime=node.getFirstChild().getNodeValue();
        System.out.println(addtime);
       }
  
        if(node.getNodeName().equals(
"pubDate")){
        String price=node.getFirstChild().getNodeValue();
        System.out.println(price);
        }   
     }
    }
    }


}

}
catch (ParserConfigurationException e) {
   e.printStackTrace();
}
     catch (FileNotFoundException e) {
   e.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}


}


public static void main(String[] args) {
    new DOMParsePage();
}

}
posted on 2009-12-26 11:08 大鸟 阅读(160) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: