Java學習

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經(jīng)搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

           

          JAVA讀取xml文件

          1. package com;   
          2.   
          3. import org.w3c.dom.*;  
          4. import javax.xml.parsers.*;  
          5. import java.io.*;  
          6.   
          7. public class Parse{  
          8.  //Document可以看作是XML在內(nèi)存中的一個鏡像,那么一旦獲取這個Document 就意味著可以通過對  
          9.  //內(nèi)存的操作來實現(xiàn)對XML的操作,首先第一步獲取XML相關的Document  
          10.   private Document doc=null;  
          11.     
          12.  public void init(String xmlFile) throws Exception{  
          13.   //很明顯該類是一個單例,先獲取產(chǎn)生DocumentBuilder工廠  
          14.   //的工廠,在通過這個工廠產(chǎn)生一個DocumentBuilder,  
          15.   //DocumentBuilder就是用來產(chǎn)生Document的  
          16.   DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();  
          17.   DocumentBuilder db=dbf.newDocumentBuilder();    
          18.   //這個Document就是一個XML文件在內(nèi)存中的鏡像  
          19.   doc=db.parse(new File(xmlFile));  
          20.  }  
          21.  //該方法負責把XML文件的內(nèi)容顯示出來  
          22.  public void viewXML(String xmlFile) throws Exception{  
          23.   this.init(xmlFile);  
          24.   //在xml文件里,只有一個根元素,先把根元素拿出來看看  
          25.   Element element=doc.getDocumentElement();  
          26.   System.out.println("根元素為:"+element.getTagName());  
          27.     
          28.   NodeList nodeList=doc.getElementsByTagName("dbstore");  
          29.   System.out.println("dbstore節(jié)點鏈的長度:"+nodeList.getLength());  
          30.     
          31.   Node fatherNode=nodeList.item(0);  
          32.   System.out.println("父節(jié)點為:"+fatherNode.getNodeName());  
          33.     
          34.   //把父節(jié)點的屬性拿出來  
          35.   NamedNodeMap attributes=fatherNode.getAttributes();  
          36.     
          37.   for(int i=0;i<attributes.getLength();i++){  
          38.    Node attribute=attributes.item(i);  
          39.    System.out.println("dbstore的屬性名為:"+attribute.getNodeName()+" 相對應的屬性值為:"+attribute.getNodeValue());  
          40.   }  
          41.   
          42.   NodeList childNodes = fatherNode.getChildNodes();  
          43.   System.out.println(childNodes.getLength());  
          44.   for(int j=0;j<childNodes.getLength();j++){  
          45.    Node childNode=childNodes.item(j);  
          46.    //如果這個節(jié)點屬于Element ,再進行取值  
          47.    if(childNode instanceof Element){  
          48.     //System.out.println("子節(jié)點名為:"+childNode.getNodeName()+"相對應的值為"+childNode.getFirstChild().getNodeValue());  
          49.     System.out.println("子節(jié)點名為:"+childNode.getNodeName()+"相對應的值為"+childNode.getFirstChild().getNodeValue());  
          50.    }  
          51.   }  
          52.     
          53.  }  
          54.    
          55.  public static void main(String[] args)throws Exception{  
          56.   Parse parse=new Parse();  
          57.   
          58. //我的XML文件  
          59.   parse.viewXML("netct.xml");  
          60.  }  
          61. }  

          posted on 2009-06-23 09:12 找個美女做老婆 閱讀(224) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          導航

          統(tǒng)計

          公告

          本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 元谋县| 米易县| 阳泉市| 广丰县| 泾源县| 遂昌县| 京山县| 安平县| 天气| 忻州市| 寿光市| 三台县| 清涧县| 兰坪| 甘泉县| 南江县| 蕲春县| 宁化县| 大安市| 碌曲县| 安龙县| 安乡县| 海口市| 卢湾区| 武功县| 鹤壁市| 浦东新区| 巴楚县| 会泽县| 柳林县| 宁陵县| 黔东| 界首市| 通江县| 西城区| 泰顺县| 城口县| 南岸区| 阳朔县| 孝昌县| 汝南县|