大鳥的學習樂園
          路漫漫其修遠兮,吾將上下而求索
          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 大鳥 閱讀(177) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 贡嘎县| 闽侯县| 紫金县| 榆社县| 巍山| 崇州市| 鄂托克旗| 岳阳县| 遂平县| 山西省| 民县| 湖北省| 名山县| 五华县| 定南县| 铁力市| 棋牌| 镇雄县| 东兰县| 兴化市| 固安县| 时尚| 清流县| 楚雄市| 鹤岗市| 偏关县| 永平县| 泾阳县| 延吉市| 柯坪县| 万载县| 名山县| 华亭县| 遂平县| 罗甸县| 南开区| 丰镇市| 沭阳县| 枣庄市| 福州市| 阿拉善左旗|