Java如何讀取XML文件 具體實(shí)現(xiàn)
今天的CSDN常見(jiàn)問(wèn)題來(lái)講解下在Java中如何讀取XML文件的內(nèi)容。
直接上代碼吧,注釋寫的很清楚了!
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;
// 可以使用絕對(duì)路勁
File f = new File("test.xml");
// documentBuilder為抽象不能直接實(shí)例化(將XML文件轉(zhuǎn)換為DOM文件)
DocumentBuilder db = null;
DocumentBuilderFactory dbf = null;
try {
// 返回documentBuilderFactory對(duì)象
dbf = DocumentBuilderFactory.newInstance();
// 返回db對(duì)象用documentBuilderFatory對(duì)象獲得返回documentBuildr對(duì)象
db = dbf.newDocumentBuilder();
// 得到一個(gè)DOM并返回給document對(duì)象
Document dt = db.parse(f);
// 得到一個(gè)elment根元素
element = dt.getDocumentElement();
// 獲得根節(jié)點(diǎn)
System.out.println("根元素:" + element.getNodeName());
// 獲得根元素下的子節(jié)點(diǎn)
NodeList childNodes = element.getChildNodes();
// 遍歷這些子節(jié)點(diǎn)
for (int i = 0; i < childNodes.getLength(); i++) {
// 獲得每個(gè)對(duì)應(yīng)位置i的結(jié)點(diǎn)
Node node1 = childNodes.item(i);
if ("Account".equals(node1.getNodeName())) {
// 如果節(jié)點(diǎn)的名稱為"Account",則輸出Account元素屬性type
System.out.println("\r\n找到一篇賬號(hào). 所屬區(qū)域: " + node1.getAttributes().getNamedItem("type").getNodeValue() + ". ");
// 獲得<Accounts>下的節(jié)點(diǎn)
NodeList nodeDetail = node1.getChildNodes();
// 遍歷<Accounts>下的節(jié)點(diǎn)
for (int j = 0; j < nodeDetail.getLength(); j++) {
// 獲得<Accounts>元素每一個(gè)節(jié)點(diǎn)
Node detail = nodeDetail.item(j);
if ("code".equals(detail.getNodeName())) // 輸出code
System.out.println("卡號(hào): " + 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();
}
}
}
然后我們測(cè)試的XML文件(test.xml)要放在項(xiàng)目工程的根目錄下,其內(nèi)容是:
<?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>
直接運(yùn)行代碼,輸出:
根元素:Accounts
找到一篇賬號(hào). 所屬區(qū)域: type1.
卡號(hào): 100001
密碼: 123
姓名: 李四
余額: 1000000.00
找到一篇賬號(hào). 所屬區(qū)域: type2.
卡號(hào): 100002
密碼: 123
姓名: 張三
余額: 1000.00
相關(guān)文章
SpringBoot+Vue.js實(shí)現(xiàn)前后端分離的文件上傳功能
這篇文章主要介紹了SpringBoot+Vue.js實(shí)現(xiàn)前后端分離的文件上傳功能,需要的朋友可以參考下2018-06-06
Java中Excel高效解析工具EasyExcel的實(shí)踐
EasyExcel是阿里巴巴開源的一個(gè)excel處理框架,已使用簡(jiǎn)單,節(jié)省內(nèi)存著稱,下面這篇文章主要給大家介紹了關(guān)于Java中Excel高效解析工具EasyExcel實(shí)踐的相關(guān)資料,需要的朋友可以參考下2022-04-04
Java中StringTokenizer的用法簡(jiǎn)介匯總
StringTokenizer?是出于兼容性的原因而被保留的遺留類(雖然在新代碼中并不鼓勵(lì)使用它),建議所有尋求此功能的人使用?String?的?split?方法或?java.util.regex?包,本文給大家整理了Java?StringTokenizer用法,感興趣的朋友一起看看吧2022-07-07
MyBatisPlus報(bào)錯(cuò):Failed to process,please exclud
這篇文章主要介紹了MyBatisPlus報(bào)錯(cuò):Failed to process,please exclude the tableName or statementId問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
詳解Springboot+React項(xiàng)目跨域訪問(wèn)問(wèn)題
這篇文章主要介紹了詳解Springboot+React項(xiàng)目跨域訪問(wèn)問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
詳解MyBatis中Executor執(zhí)行SQL語(yǔ)句的過(guò)程
MyBatis中獲取SqlSession時(shí)會(huì)創(chuàng)建執(zhí)行器Executor并存放在SqlSession中,本篇文章將以MapperMethod的execute() 方法作為起點(diǎn),對(duì)MyBatis中的一次實(shí)際執(zhí)行請(qǐng)求進(jìn)行說(shuō)明,并結(jié)合源碼對(duì)執(zhí)行器Executor的原理進(jìn)行闡釋2023-07-07
解決IDEA2021版compiler.automake.allow.when.app.running不存在的問(wèn)題
很多文章介紹IntelliJ IDEA開啟熱部署功能都會(huì)寫到在IntelliJ IDEA中的注冊(cè)表中開啟compiler.automake.allow.when.app.running選項(xiàng),此選項(xiàng)在IntelliJ IDEA 2021.2之后的版本遷移到高級(jí)設(shè)置中,下面看下設(shè)置方法2021-09-09

