Java學(xué)習(xí)

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

           

          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)存中的一個(gè)鏡像,那么一旦獲取這個(gè)Document 就意味著可以通過對(duì)  
          9.  //內(nèi)存的操作來實(shí)現(xiàn)對(duì)XML的操作,首先第一步獲取XML相關(guān)的Document  
          10.   private Document doc=null;  
          11.     
          12.  public void init(String xmlFile) throws Exception{  
          13.   //很明顯該類是一個(gè)單例,先獲取產(chǎn)生DocumentBuilder工廠  
          14.   //的工廠,在通過這個(gè)工廠產(chǎn)生一個(gè)DocumentBuilder,  
          15.   //DocumentBuilder就是用來產(chǎn)生Document的  
          16.   DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();  
          17.   DocumentBuilder db=dbf.newDocumentBuilder();    
          18.   //這個(gè)Document就是一個(gè)XML文件在內(nèi)存中的鏡像  
          19.   doc=db.parse(new File(xmlFile));  
          20.  }  
          21.  //該方法負(fù)責(zé)把XML文件的內(nèi)容顯示出來  
          22.  public void viewXML(String xmlFile) throws Exception{  
          23.   this.init(xmlFile);  
          24.   //在xml文件里,只有一個(gè)根元素,先把根元素拿出來看看  
          25.   Element element=doc.getDocumentElement();  
          26.   System.out.println("根元素為:"+element.getTagName());  
          27.     
          28.   NodeList nodeList=doc.getElementsByTagName("dbstore");  
          29.   System.out.println("dbstore節(jié)點(diǎn)鏈的長(zhǎng)度:"+nodeList.getLength());  
          30.     
          31.   Node fatherNode=nodeList.item(0);  
          32.   System.out.println("父節(jié)點(diǎn)為:"+fatherNode.getNodeName());  
          33.     
          34.   //把父節(jié)點(diǎn)的屬性拿出來  
          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()+" 相對(duì)應(yīng)的屬性值為:"+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.    //如果這個(gè)節(jié)點(diǎn)屬于Element ,再進(jìn)行取值  
          47.    if(childNode instanceof Element){  
          48.     //System.out.println("子節(jié)點(diǎn)名為:"+childNode.getNodeName()+"相對(duì)應(yīng)的值為"+childNode.getFirstChild().getNodeValue());  
          49.     System.out.println("子節(jié)點(diǎn)名為:"+childNode.getNodeName()+"相對(duì)應(yīng)的值為"+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 找個(gè)美女做老婆 閱讀(225) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          公告

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

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 石棉县| 连城县| 清水河县| 娱乐| 长武县| 鲁甸县| 大悟县| 临猗县| 诸城市| 孟村| 休宁县| 双牌县| 静宁县| 漾濞| 讷河市| 浮梁县| 镶黄旗| 合水县| 香格里拉县| 抚松县| 盘锦市| 东乡族自治县| 临漳县| 工布江达县| 策勒县| 和平县| 桦川县| 营山县| 铜陵市| 叶城县| 东至县| 红河县| 邵阳县| 余姚市| 青神县| 施秉县| 台南市| 东辽县| 额济纳旗| 湖口县| 宣恩县|