lushengdi
posts - 75, comments - 13, trackbacks - 0, articles - 0
BlogJava
首页
新随笔
联系
管理
聚合
<
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
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔分类
Hibernate(3)
JBPM(2)
lucene(1)
一点点(15)
系统防卫(3)
问题集(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)
搜索
最新评论
1. re: Named query not known(解决)
我也遇到了相同的问题,我真的很想知道答案,我的项目碰到了,我真的很急啊,希望那位高人能帮帮我啊
--xiaozi
2. re: javascript传值给jsp 简单实例[未登录]
评论内容较长,点击标题查看
--DIVSYSTEM
3. re: Tomcat 6免安装版 配置 问题[未登录]
评论内容较长,点击标题查看
--DIVSYSTEM
4. re: Named query not known(解决)
sql是写在代码里的
--sdsss
5. re: Named query not known(解决)
等于没说
--sdsss
阅读排行榜
1. oracle创建表空间,创建用户以及授权(995)
2. SQL SERVER2000安装方法及下载地址(952)
3. Hibernate学习笔记(874)
4. javascript传值给jsp 简单实例(827)
5. Spring学习笔记(786)
评论排行榜
1. Lucene开发环境配置及Demo调试(3)
2. Named query not known(解决)(3)
3. JSP连接各数据库实例(2)
4. 使浏览器能自动下载JVM的方法(1)
5. javascript传值给jsp 简单实例(1)
60天内阅读排行
1. Tomcat 6免安装版 配置 问题(168)
2. jbpm-starters-kit-3.1.2.zip官方下载地址(150)
3. 网友称微软黑屏系违法 黑屏补丁已被破解(图)(112)
4. 如何卸载Windows服务!(104)
5. MySQL 6 绿色精简BAT版 下载 (99)
2008年3月4日
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 @
2008-03-04 10:04
鲁胜迪 阅读(154) |
评论 (0)
|
编辑
收藏