欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java如何讀取XML文件 具體實現(xiàn)

 更新時間:2013年12月11日 15:25:57   作者:  
這篇文章主要介紹了Java如何讀取XML文件 具體實現(xiàn),有需要的朋友可以參考一下

今天的CSDN常見問題來講解下在Java中如何讀取XML文件的內(nèi)容。

直接上代碼吧,注釋寫的很清楚了!

復(fù)制代碼 代碼如下:

import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReaderTest {

 public static void main(String args[]) {
  Element element = null;
  // 可以使用絕對路勁
  File f = new File("test.xml");

  // documentBuilder為抽象不能直接實例化(將XML文件轉(zhuǎn)換為DOM文件)
  DocumentBuilder db = null;
  DocumentBuilderFactory dbf = null;
  try {
   // 返回documentBuilderFactory對象
   dbf = DocumentBuilderFactory.newInstance();
   // 返回db對象用documentBuilderFatory對象獲得返回documentBuildr對象
   db = dbf.newDocumentBuilder();

   // 得到一個DOM并返回給document對象
   Document dt = db.parse(f);
   // 得到一個elment根元素
   element = dt.getDocumentElement();
   // 獲得根節(jié)點
   System.out.println("根元素:" + element.getNodeName());

   // 獲得根元素下的子節(jié)點
   NodeList childNodes = element.getChildNodes();

   // 遍歷這些子節(jié)點
   for (int i = 0; i < childNodes.getLength(); i++) {
    // 獲得每個對應(yīng)位置i的結(jié)點
    Node node1 = childNodes.item(i);
    if ("Account".equals(node1.getNodeName())) {
     // 如果節(jié)點的名稱為"Account",則輸出Account元素屬性type
     System.out.println("\r\n找到一篇賬號. 所屬區(qū)域: " + node1.getAttributes().getNamedItem("type").getNodeValue() + ". ");
     // 獲得<Accounts>下的節(jié)點
     NodeList nodeDetail = node1.getChildNodes();
     // 遍歷<Accounts>下的節(jié)點
     for (int j = 0; j < nodeDetail.getLength(); j++) {
      // 獲得<Accounts>元素每一個節(jié)點
      Node detail = nodeDetail.item(j);
      if ("code".equals(detail.getNodeName())) // 輸出code
       System.out.println("卡號: " + detail.getTextContent());
      else if ("pass".equals(detail.getNodeName())) // 輸出pass
       System.out.println("密碼: " + detail.getTextContent());
      else if ("name".equals(detail.getNodeName())) // 輸出name
       System.out.println("姓名: " + detail.getTextContent());
      else if ("money".equals(detail.getNodeName())) // 輸出money
       System.out.println("余額: " + detail.getTextContent());
     }
    }

   }
  }

  catch (Exception e) {
   e.printStackTrace();
  }
 }
}

然后我們測試的XML文件(test.xml)要放在項目工程的根目錄下,其內(nèi)容是:

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="gbk"?>
<Accounts>
 <Account type="type1">
  <code>100001</code>
  <pass>123</pass>
  <name>李四</name>
  <money>1000000.00</money>
 </Account>
 <Account type="type2">
  <code>100002</code>
  <pass>123</pass>
  <name>張三</name>
  <money>1000.00</money>
 </Account>
</Accounts>

直接運行代碼,輸出:

根元素:Accounts

找到一篇賬號. 所屬區(qū)域: type1.
卡號: 100001
密碼: 123
姓名: 李四
余額: 1000000.00

找到一篇賬號. 所屬區(qū)域: type2.
卡號: 100002
密碼: 123
姓名: 張三
余額: 1000.00

相關(guān)文章

  • SpringCache源碼解析Annotation案例講解

    SpringCache源碼解析Annotation案例講解

    這篇文章主要介紹了SpringCache源碼解析Annotation的相關(guān)知識,本文通過案例講解的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能

    SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能

    這篇文章主要介紹了SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能,需要的朋友可以參考下
    2018-06-06
  • Java中Excel高效解析工具EasyExcel的實踐

    Java中Excel高效解析工具EasyExcel的實踐

    EasyExcel是阿里巴巴開源的一個excel處理框架,已使用簡單,節(jié)省內(nèi)存著稱,下面這篇文章主要給大家介紹了關(guān)于Java中Excel高效解析工具EasyExcel實踐的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • SpringBoot上傳下載文件+oss實例

    SpringBoot上傳下載文件+oss實例

    這篇文章主要介紹了SpringBoot上傳下載文件+oss實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Maven 主模塊和子模塊pom.xml依賴聲明

    Maven 主模塊和子模塊pom.xml依賴聲明

    這篇文章主要介紹了Maven 主模塊和子模塊pom.xml依賴聲明,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Java中StringTokenizer的用法簡介匯總

    Java中StringTokenizer的用法簡介匯總

    StringTokenizer?是出于兼容性的原因而被保留的遺留類(雖然在新代碼中并不鼓勵使用它),建議所有尋求此功能的人使用?String?的?split?方法或?java.util.regex?包,本文給大家整理了Java?StringTokenizer用法,感興趣的朋友一起看看吧
    2022-07-07
  • MyBatisPlus報錯:Failed to process,please exclude the tableName or statementId問題

    MyBatisPlus報錯:Failed to process,please exclud

    這篇文章主要介紹了MyBatisPlus報錯:Failed to process,please exclude the tableName or statementId問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解Springboot+React項目跨域訪問問題

    詳解Springboot+React項目跨域訪問問題

    這篇文章主要介紹了詳解Springboot+React項目跨域訪問問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 詳解MyBatis中Executor執(zhí)行SQL語句的過程

    詳解MyBatis中Executor執(zhí)行SQL語句的過程

    MyBatis中獲取SqlSession時會創(chuàng)建執(zhí)行器Executor并存放在SqlSession中,本篇文章將以MapperMethod的execute() 方法作為起點,對MyBatis中的一次實際執(zhí)行請求進(jìn)行說明,并結(jié)合源碼對執(zhí)行器Executor的原理進(jìn)行闡釋
    2023-07-07
  • 解決IDEA2021版compiler.automake.allow.when.app.running不存在的問題

    解決IDEA2021版compiler.automake.allow.when.app.running不存在的問題

    很多文章介紹IntelliJ IDEA開啟熱部署功能都會寫到在IntelliJ IDEA中的注冊表中開啟compiler.automake.allow.when.app.running選項,此選項在IntelliJ IDEA 2021.2之后的版本遷移到高級設(shè)置中,下面看下設(shè)置方法
    2021-09-09

最新評論