讀取xml文檔的節(jié)點(diǎn)文本值
------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)建解析器工廠對(duì)象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//2.通過解析器工廠對(duì)象創(chuàng)建解析器對(duì)象
DocumentBuilder db = dbf.newDocumentBuilder();
//3.通過解析器對(duì)象解析xml文檔,并創(chuàng)建doc對(duì)象
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();
}
posted on 2014-11-01 11:38 阿九 閱讀(276) 評(píng)論(0) 編輯 收藏 所屬分類: XML