更新時(shí)間:2021-08-19 11:37:37 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1200次
-----show.xml
<?xml version="1.0" encoding="GB2312" standalone="no"?>
<PhoneInfo>
<Brand name="華為">
<Type name="U8650">
<Item>
<title>標(biāo)題信息</title>
<link>鏈接</link>
<description>標(biāo)題信息</description>
<pubDate>2014-11-1</pubDate>
</Item>
</Type>
<Type name="HW123"/>
<Type name="HW321"/>
</Brand>
<Brand name="蘋果">
<Type name="iPhone4"/>
</Brand>
</PhoneInfo>
--------test.java
try{
//1創(chuàng)建解析器工廠對象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//2.通過解析器工廠對象創(chuàng)建解析器對象
DocumentBuilder db = dbf.newDocumentBuilder();
//3.通過解析器對象解析xml文檔,并創(chuàng)建doc對象
Document doc = db.parse("src/show.xml");
//4.讀取pubDate
NodeList list = doc.getElementsByTagName("pubDate");
//4.1讀取pubDate的元素節(jié)點(diǎn)
Element pubElement =(Element) list.item(0);
//4.2讀取pubDate的文本節(jié)點(diǎn)
String pubDateValue = pubElement.getFirstChild().getNodeValue();
System.out.println(pubDateValue);
}catch(Exception e){
e.printStackTrace();
}
以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"讀取XML文檔的節(jié)點(diǎn)文本值",希望對大家有幫助,想了解更多可查看Java在線學(xué)習(xí)。動(dòng)力節(jié)點(diǎn)在線學(xué)習(xí)教程,針對沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識(shí),讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。