讀取xml文檔的節點文本值
------show.xml<?xml version="1.0" encoding="GB2312" standalone="no"?>
<PhoneInfo>
<Brand name="華為">
<Type name="U8650">
<Item>
<title>標題信息</title>
<link>鏈接</link>
<description>標題信息</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創建解析器工廠對象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//2.通過解析器工廠對象創建解析器對象
DocumentBuilder db = dbf.newDocumentBuilder();
//3.通過解析器對象解析xml文檔,并創建doc對象
Document doc = db.parse("src/show.xml");
//4.讀取pubDate
NodeList list = doc.getElementsByTagName("pubDate");
//4.1讀取pubDate的元素節點
Element pubElement =(Element) list.item(0);
//4.2讀取pubDate的文本節點
String pubDateValue = pubElement.getFirstChild().getNodeValue();
System.out.println(pubDateValue);
}catch(Exception e){
e.printStackTrace();
}
posted @ 2014-11-01 11:38 阿九 閱讀(275) | 評論 (0) | 編輯 收藏