溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          xml文件
          <?xml version="1.0" encoding="UTF-8"?>
          <data>
              
          <book id="1">
                  
          <name>Android應用開發詳解</name>
                  
          <author>json</author>
                  
          <price>88</price>
                  
          <pubinfo>人民郵電出版社</pubinfo>
              
          </book>
              
          <book id="2">
                  
          <name>Android權威指南</name>
                  
          <author>tom</author>
                  
          <price>79</price>
                  
          <pubinfo>人民教育出版社</pubinfo>
              
          </book>
              
          <book id="3">
                  
          <name>Android開發案例大全</name>
                  
          <author>mark</author>
                  
          <price>68</price>
                  
          <pubinfo>電子工業出版社</pubinfo>
              
          </book>
              
          <book id="4">
                  
          <name>Android從入門到精通</name>
                  
          <author>jack</author>
                  
          <price>68</price>
                  
          <pubinfo>電子工業出版社</pubinfo>
              
          </book>
              
          <book id="5">
                  
          <name>Pro Spring</name>
                  
          <author>mark</author>
                  
          <price>68</price>
                  
          <pubinfo>電子工業出版社</pubinfo>
              
          </book>
          </data>
          解析類
          package com.dom;

          import java.io.File;
          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.Element;
          import org.w3c.dom.NodeList;
          import org.xml.sax.SAXException;

          /**
           * <pre>
           * dom解析xml
           * <pre>
           * 
          @author scott
           *
           
          */
          public class DomXmlParser {

              
          public static void main(String[] args) {
                  DocumentBuilderFactory factory
          =DocumentBuilderFactory.newInstance();
                  File file
          =new File("D:\\workspace\\demo\\src\\books.xml");
                  DocumentBuilder documentBuilder
          =null;
                  
          try {
                      documentBuilder 
          = factory.newDocumentBuilder();
                  } 
          catch (ParserConfigurationException e) {
                      e.printStackTrace();
                  }
                  Document document
          =null;
                  
          try {
                      document
          =documentBuilder.parse(file);
                  } 
          catch (SAXException e) {
                      e.printStackTrace();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  
                  Element element
          =document.getDocumentElement();
                  NodeList nodeList
          =element.getElementsByTagName("book");
                  
          for (int i = 0; i < nodeList.getLength(); i++) {
                      Element book 
          = (Element)nodeList.item(i);
                      String id
          =book.getAttribute("id");
                      
                      Element bookname
          =(Element) book.getElementsByTagName("name").item(0);
                      String name
          =bookname.getFirstChild().getNodeValue();
                      
                      Element bookauthor
          =(Element) book.getElementsByTagName("author").item(0);
                      String author
          =bookauthor.getFirstChild().getNodeValue();
                      
                      Element bookprice
          =(Element) book.getElementsByTagName("price").item(0);
                      String price
          =bookprice.getFirstChild().getNodeValue();
                      
                      Element bookpubinfo
          =(Element) book.getElementsByTagName("pubinfo").item(0);
                      String pubinfo
          =bookpubinfo.getFirstChild().getNodeValue();
                      
                      System.out.println(id
          +","+name+","+author+","+price+","+pubinfo);
                      
                  }
                  
                  
                  
              }

          }
          效果
          1,Android應用開發詳解,json,88,人民郵電出版社
          2,Android權威指南,tom,79,人民教育出版社
          3,Android開發案例大全,mark,68,電子工業出版社
          4,Android從入門到精通,jack,68,電子工業出版社
          5,Pro Spring,mark,68,電子工業出版社

          posted on 2011-08-24 11:32 雪山飛鵠 閱讀(439) 評論(0)  編輯  收藏 所屬分類: javaseandroid
          主站蜘蛛池模板: 旅游| 金溪县| 景德镇市| 江门市| 邵东县| 民和| 凤台县| 卓尼县| 砀山县| 来宾市| 江北区| 吴忠市| 福建省| 绥中县| 怀远县| 阳城县| 鹤壁市| 福清市| 和林格尔县| 合肥市| 深州市| 阿克陶县| 曲麻莱县| 祁连县| 汨罗市| 达尔| 英德市| 乌拉特中旗| 巴彦淖尔市| 汾西县| 广南县| 新沂市| 桐梓县| 沧源| 桐柏县| 长葛市| 许昌市| 潼南县| 体育| 滦南县| 凤城市|