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

          雪山飛鵠

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

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

          公告

          Locations of visitors to this page

          常用鏈接

          留言簿(18)

          隨筆分類(222)

          隨筆檔案(214)

          文章檔案(1)

          收藏夾(67)

          我的鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          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
          主站蜘蛛池模板: 文化| 临清市| 醴陵市| 武义县| 奉新县| 郧西县| 巴林右旗| 怀宁县| 新闻| 泗洪县| 佛山市| 二连浩特市| 咸宁市| 寻甸| 徐州市| 汝州市| 宣化县| 芜湖市| 江永县| 大化| 前郭尔| 景东| 名山县| 祁连县| 峨山| 涟源市| 奉化市| 连江县| 东阳市| 元阳县| 福安市| 麻栗坡县| 西昌市| 绥化市| 潍坊市| 丰台区| 晋江市| 林州市| 德钦县| 贡觉县| 深水埗区|