漂在爪洼島上

          Java is my life,but not all!
          posts - 3, comments - 6, trackbacks - 0, articles - 9
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          JAVA 解析XML文檔示例

          Posted on 2009-06-19 09:16 周競先 閱讀(425) 評(píng)論(0)  編輯  收藏 所屬分類: J2EExml
          根據(jù)上一篇文章構(gòu)造的XML進(jìn)行簡單的解析,代碼如下:
           1 package com.potevio.telecom;
           2 
           3 //文件類
           4 import java.io.File;
           5 
           6 //負(fù)責(zé)解析的類
           7 import javax.xml.parsers.DocumentBuilder;
           8 import javax.xml.parsers.DocumentBuilderFactory;
           9 
          10 //節(jié)點(diǎn)類
          11 import org.w3c.dom.Document;
          12 import org.w3c.dom.NodeList;
          13 
          14 /**
          15  * @description 解析"北京到長沙的簡單列車時(shí)刻表"信息
          16  * 
          17  * @author Zhou-Jingxian
          18  * 
          19  * @date Jun 18, 2009
          20  *
          21  */
          22 public class ParserXML {
          23 
          24     public static void main(String[] args) {
          25         
          26         try{
          27             //需要解析的文件
          28             File file = new File("北京到長沙火車時(shí)刻表.xml");
          29             
          30             //解析器工廠類
          31             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          32             
          33             //解析器
          34             DocumentBuilder builder = factory.newDocumentBuilder();
          35             
          36             //操作的Document對(duì)象
          37             Document document = builder.parse(file);
          38             
          39             //節(jié)點(diǎn)名稱
          40             NodeList nodelist = document.getElementsByTagName("車次");
          41             
          42             //解析內(nèi)容
          43             for(int i = 0; i<nodelist.getLength(); i++){
          44                 System.out.println("--------"+(i+1)+"---------");
          45                 System.out.println("車類別:"+document.getElementsByTagName("車次").item(i).getAttributes().getNamedItem("類別").getNodeValue());
          46                 System.out.println("車次號(hào):"+document.getElementsByTagName("名字").item(i).getFirstChild().getNodeValue());
          47                 System.out.println("開車時(shí)間:"+document.getElementsByTagName("開車時(shí)間").item(i).getFirstChild().getNodeValue());
          48                 
          49             }
          50         }catch(Exception e){
          51             e.printStackTrace();
          52             
          53         }finally{
          54             
          55         }
          56     }
          57 }
          58 

          運(yùn)行結(jié)果如下:
          解析xml結(jié)果圖

          Life,simple and happy!

          主站蜘蛛池模板: 庆安县| 平舆县| 石城县| 红原县| 华蓥市| 如皋市| 莫力| 吉安市| 海丰县| 巩留县| 宜丰县| 社旗县| 临湘市| 西城区| 邮箱| 静乐县| 芜湖县| 顺昌县| 松溪县| 永和县| 星座| 噶尔县| 闽侯县| 淮北市| 昂仁县| 贵阳市| 民权县| 噶尔县| 东阿县| 额尔古纳市| 赞皇县| 长宁县| 全椒县| 台山市| 克什克腾旗| 洪雅县| 鲁甸县| 道真| 涞源县| 明光市| 邹城市|