Zhu Jun's Tech Weblog
首頁
新文章
新隨筆
聚合
管理
posts - 54, comments - 1, trackbacks - 0
使用JDom和XPath簡單訪問XML文件
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
/**/
/*
*
* 使用JDom操作XML工具
*
* @author ZhuJun Create on 2005-8-17
*/
public
class
XmlUtil
{
/**/
/*
*
* 解析XML文件,生成Document對象
*
* @param fileName
* @return @throws
* JDOMException
* @throws IOException
*
* @author ZhuJun Create on 2005-8-17
*/
public
static
Document parseXml(String fileName) throws JDOMException,
IOException
{
SAXBuilder builder
=
new
SAXBuilder();
return
builder.build(fileName);
}
/**/
/*
*
* 輸出XML文件
*
* @param docXML
* @param strFilename
*
* @author ZhuJun Create on 2005-8-17
*/
public
static
void
OutputXML(Document doc, String strFilename)
{
XMLOutputter fmt
=
new
XMLOutputter();
fmt.setFormat(Format.getPrettyFormat());
try
{
FileWriter fwXML
=
new
FileWriter(strFilename);
fmt.output(doc, fwXML);
fwXML.close();
}
catch
(IOException e)
{
e.printStackTrace();
}
}
/**/
/*
*
* 取得節(jié)點(diǎn)的值
*
* @param xPath
* @return @throws
* JDOMException
*
* @author ZhuJun Create on 2005-8-17
*/
public
static
String getNodeValue(Document doc, String xPath)
throws JDOMException
{
return
((Text) XPath.selectSingleNode(doc.getRootElement(),
new
StringBuffer(xPath).append(
"
/text()
"
).toString()))
.getTextNormalize();
}
/**/
/*
*
* 取得節(jié)點(diǎn)的屬性值
*
* @param xPath
* @param attrName
* @return @throws
* JDOMException
*
* @author ZhuJun Create on 2005-8-17
*/
public
static
String getAttributeValue(Document doc, String xPath,
String attrName) throws JDOMException
{
Element element
=
(Element) XPath.selectSingleNode(
doc.getRootElement(), xPath);
return
element.getAttributeValue(attrName);
}
}
posted on 2005-08-18 14:12
ZhuJun
閱讀(344)
評論(0)
編輯
收藏
所屬分類:
開發(fā)手記
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
[Struts] 使用多個(gè)資源文件
[Tomcat] 修改Tomcat端口號
[JDBC] 常用JDBC連接字符串
[JDBC] DatabaseMetaDate接口
[Velocity] Velocity 參考資料
[Eclipse] 在Eclipse中安裝Visual Editor插件
[Velocity] Velocity中遇到的一個(gè)問題
[Hibernate] 為Hibernate提供JDBC連接
[發(fā)掘Jakarta Commons] 使用StringUtil類
[Oracle] Oracle中的dual表
蜀中人氏,躬耕于珠海
<
2005年8月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆分類
(71)
Database(4)
Java基礎(chǔ)(2)
WEB相關(guān)(2)
他山の玉(12)
開發(fā)手記(34)
開源項(xiàng)目(10)
閑言閑語(7)
隨筆檔案
(54)
2006年1月 (9)
2005年12月 (13)
2005年11月 (6)
2005年9月 (8)
2005年8月 (16)
2005年7月 (2)
博客
【方道?文山流】
David的程序人生
Google與李開復(fù)博士
KINGFISH和蝸牛的家
棋子驛站
竹筍炒肉
良葛格
車東
文檔
Hibernate參考文檔
Javascript Toolbox
Movable Type 完全手冊
MySQL中文參考手冊
Spring Framework 參考手冊
Struts API在線文檔
SWT Tutorial
滿江紅.開源
英語寶盒 | English Study
站點(diǎn)
BEA dev2dev
developerWorks
IBM中國
Java開源大全
Java愛好者
Java視線
Matrix
SourceForge
SUN 中國技術(shù)社區(qū)
The Apache Software Foundation
Theserverside
中國手機(jī)網(wǎng)
灰狐動(dòng)力
論壇
hibernate論壇
JavaWorld
Java視線
Matrix
spring英文論壇
中國Java開發(fā)網(wǎng)
搜索
積分與排名
積分 - 51232
排名 - 974
最新評論
1.?re: 剔除ArrayList中的重復(fù)值
根本不好用
--nf
閱讀排行榜
1.?could not do a physical order read to fetch next row(5416)
2.?[Tomcat] 修改Tomcat端口號(4037)
3.?Servlet 上傳文件(轉(zhuǎn))(3734)
4.?剔除ArrayList中的重復(fù)值(2010)
5.?[Oracle] Oracle exp/imp導(dǎo)出導(dǎo)入工具的使用 [轉(zhuǎn)] (1698)
Copyright ©2025 ZhuJun Powered By
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
宜兰市
|
海阳市
|
黔江区
|
桃园县
|
南丰县
|
阿克陶县
|
铜梁县
|
东源县
|
荔波县
|
平谷区
|
富裕县
|
襄城县
|
遵义市
|
遂宁市
|
铜川市
|
黔西县
|
宜城市
|
海南省
|
萨迦县
|
班戈县
|
万宁市
|
樟树市
|
梨树县
|
南投县
|
北碚区
|
青阳县
|
定边县
|
临沭县
|
馆陶县
|
那坡县
|
孟村
|
青岛市
|
卢龙县
|
桓台县
|
湟中县
|
那曲县
|
巨鹿县
|
岱山县
|
田林县
|
安龙县
|
礼泉县
|