Java如何讀取XML文件 具體實現(xiàn)
今天的CSDN常見問題來講解下在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;
// 可以使用絕對路勁
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)容是:
<?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)文章
SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能
這篇文章主要介紹了SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能,需要的朋友可以參考下2018-06-06
MyBatisPlus報錯:Failed to process,please exclud
這篇文章主要介紹了MyBatisPlus報錯:Failed to process,please exclude the tableName or statementId問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
詳解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不存在的問題
很多文章介紹IntelliJ IDEA開啟熱部署功能都會寫到在IntelliJ IDEA中的注冊表中開啟compiler.automake.allow.when.app.running選項,此選項在IntelliJ IDEA 2021.2之后的版本遷移到高級設(shè)置中,下面看下設(shè)置方法2021-09-09

