lushengdi
JDom使用詳解
JDom是不錯的API,算得上簡單高效,最重要是已經成為jcp的一部分,這個咱得弄弄。不過www.jdom.org上寫文檔的人實在太懶,文檔出奇的少,流傳得最廣的恐怕是IBM上面的一篇《JDom讓java XML變得容易》,不過這篇文章只涉及基本的讀寫操作,遠不能勝任實際工作。花了兩天時間,把JDom的基本操作整理出來了,涵蓋了大部分的操作:元素、屬性、命名空間、PI、DTD、Schema,應付一般的應用沒什么問題。反正我沒有在網上見到更加詳盡的版本,你見過的話,請留下連接。暫時來不及編寫詳細的說明,先帖幾段程序,對有經驗的Java開發者來說,已經足夠了。程序都已經經過了實際的測試,我使用的JDom是0.9版。
1
、創建XML文檔:
package
org.bromon.jdom.example;
import
java.io.
*
;
import
org.jdom.
*
;
import
org.jdom.input.
*
;
import
org.jdom.output.
*
;
public
class
CreateXML
{
public
void
Create()
{
try
{
Document doc
=
new
Document();
ProcessingInstruction pi
=
new
ProcessingInstruction(
"
xml-stylesheet
"
,
"
type=
"
textxsl
"
href=
"
test.xsl
""
);
doc.addContent(pi);
Namespace ns
=
Namespace.getNamespace(
"
http://www.bromon.org
"
);
Namespace ns2
=
Namespace.getNamespace(
"
other
"
,
"
http://www.w3c.org
"
);
Element root
=
new
Element(
"
根元素
"
, ns);
root.addNamespaceDeclaration(ns2);
doc.setRootElement(root);
Element el1
=
new
Element(
"
元素一
"
);
el1.setAttribute(
"
屬性
"
,
"
屬性一
"
);
Text text1
=
new
Text(
"
元素值
"
);
Element em
=
new
Element(
"
元素二
"
).addContent(
"
第二個元素
"
);
el1.addContent(text1);
el1.addContent(em);
Element el2
=
new
Element(
"
元素三
"
).addContent(
"
第三個元素
"
);
root.addContent(el1);
root.addContent(el2);
//
縮進四個空格,自動換行,gb2312編碼
XMLOutputter outputter
=
new
XMLOutputter(
"
"
,
true
,
"
GB2312
"
);
outputter.output(doc,
new
FileWriter(
"
test.xml
"
));
}
catch
(Exception e)
{
System.out.println(e);
}
}
public
static
void
main(String args[])
{
new
CreateXML().Create();
}
}
2
、DTD驗證的:
package
org.bromon.jdom.example;
import
java.io.
*
;
import
org.jdom.
*
;
import
org.jdom.input.
*
;
import
org.jdom.output.
*
;
public
class
XMLWithDTD
{
public
void
validate()
{
try
{
SAXBuilder builder
=
new
SAXBuilder(
true
);
builder.setFeature(
"
http://xml.org/sax/features/validation
"
;,
true
);
Document doc
=
builder.build(
new
FileReader(
"
author.xml
"
));
System.out.println(
"
搞掂
"
);
XMLOutputter outputter
=
new
XMLOutputter();
outputter.output(doc, System.out);
}
catch
(Exception e)
{
System.out.println(e);
}
}
public
static
void
main(String args[])
{
new
XMLWithDTD().validate();
}
}
需要說明的是,這個程序沒有指明使用哪個DTD文件。DTD文件的位置是在XML中指定的,而且DTD不支持命名空間,一個XML只能引用一個DTD,所以程序直接讀取XML中指定的DTD,程序本身不用指定。不過這樣一來,好象就只能使用外部式的DTD引用方式了?高人指點。
3
、XML Schema驗證的:
package
org.bromon.jdom.example;
import
java.io.
*
;
import
org.jdom.
*
;
import
org.jdom.input.
*
;
import
org.jdom.output.
*
;
public
class
XMLWithSchema
{
String xml
=
"
test.xml
"
;
String schema
=
"
test-schema.xml
"
;
public
void
validate()
{
try
{
SAXBuilder builder
=
new
SAXBuilder(
true
);
//
指定約束方式為XML schema
builder.setFeature(
"
http://apache.org/xml/features/validation/schema
"
;,
true
);
//
導入schema文件
builder.setProperty(
"
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation
"
;,schema);
Document doc
=
builder.build(
new
FileReader(xml));
System.out.println(
"
搞掂
"
);
XMLOutputter outputter
=
new
XMLOutputter();
outputter.output(doc, System.out);
}
catch
(Exception e)
{
System.out.println(
"
驗證失敗:
"
+
e);
}
}
}
posted on 2008-03-04 10:04
魯勝迪
閱讀(728)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 魯勝迪
<
2008年3月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
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
導航
BlogJava
首頁
新隨筆
聯系
聚合
管理
統計
隨筆 - 122
文章 - 0
評論 - 89
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(4)
給我留言
查看公開留言
查看私人留言
隨筆分類
Flex(1)
(rss)
Hibernate(4)
(rss)
JBPM(2)
(rss)
lucene(1)
(rss)
Play Framework(1)
(rss)
一點點(23)
(rss)
系統防衛(3)
(rss)
問題集(3)
(rss)
隨筆檔案
2015年1月 (1)
2014年11月 (1)
2013年11月 (1)
2013年7月 (1)
2013年2月 (2)
2013年1月 (1)
2012年9月 (2)
2012年8月 (2)
2012年5月 (2)
2012年4月 (1)
2012年3月 (2)
2012年2月 (2)
2011年12月 (3)
2011年6月 (2)
2010年12月 (1)
2010年9月 (2)
2010年7月 (4)
2010年4月 (1)
2010年1月 (2)
2009年11月 (2)
2009年8月 (1)
2009年7月 (2)
2009年6月 (2)
2009年2月 (1)
2009年1月 (2)
2008年12月 (3)
2008年11月 (2)
2008年10月 (7)
2008年9月 (7)
2008年8月 (6)
2008年7月 (9)
2008年6月 (5)
2008年5月 (5)
2008年4月 (5)
2008年3月 (11)
2008年2月 (2)
2008年1月 (6)
2007年12月 (3)
文章分類
FLEX
(rss)
新聞分類
J-Hi
(rss)
搜索
最新評論
1.?re: Mysql 免安裝 配置步驟
很好
--劉梅
2.?re: Mysql 免安裝 配置步驟
不錯
--劉梅
3.?re: javascript傳值給jsp 簡單實例
11
--11
4.?re: Myeclipse10下載,安裝,破解,插件,優化介紹
好用
--嚴夢婷
5.?re: JSF 帶參數 頁面重定向
謝謝啊是到底
--阿薩
閱讀排行榜
1.?oracle exp/imp 導入導出命令(53957)
2.?Tomcat(免安裝版)的安裝與配置 配置成windows服務(22254)
3.?oracle創建表空間,創建用戶以及授權(21092)
4.?plsql developer 下載、注冊及破解方法(18694)
5.?Myeclipse10下載,安裝,破解,插件,優化介紹(15799)
評論排行榜
1.?plsql developer 下載、注冊及破解方法(16)
2.?oracle創建表空間,創建用戶以及授權(12)
3.?jbpm-starters-kit-3.1.2.zip官方下載地址(8)
4.?Named query not known(解決)(6)
5.?Myeclipse10下載,安裝,破解,插件,優化介紹(5)
主站蜘蛛池模板:
武汉市
|
紫阳县
|
荣昌县
|
泰州市
|
凤庆县
|
安化县
|
纳雍县
|
旬邑县
|
高安市
|
寻乌县
|
监利县
|
万年县
|
青岛市
|
福海县
|
平罗县
|
新沂市
|
庆元县
|
昌平区
|
通州区
|
雷波县
|
光泽县
|
新沂市
|
革吉县
|
新宁县
|
云和县
|
石林
|
自贡市
|
翁牛特旗
|
健康
|
开平市
|
津南区
|
惠安县
|
渝中区
|
定南县
|
昭平县
|
新龙县
|
格尔木市
|
石家庄市
|
明溪县
|
绥芬河市
|
林州市
|