<%@ page contentType="text/html; charset=gb2312"%> JDom使用详解
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
JDom使用详解
     发布者: 发布时间:2007-09-23
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);
}

}

}
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
SQL Server数据库Java驱动jTDS发布1.1版本
Oracle9i PL/SQL编程的经验小结
在Java SE中使用Hibernate框架
Java程序员的存储过程
Java程序员的存储过程
JDBC数据库连接的问题
 
最近评论:
        
鍥炲
        
那个雨天的想法!
wow gold,wow power leveling.wow power leveling,wow power leveling, max(3819)
        
那天的情景!
Maple Story mesos,MapleStory mesos,ms mesos,mesos,SilkRoad Gold, max(5434)
        
轻轻走过你的窗前!
world of warcraft gold,cheap world of warcraft gold,warcraft gold,world of warcraft gold,cheap world of warcraft gold,warcraft gold, max(2101)
        
轻轻走过你的窗前!
world of warcraft gold,cheap world of warcraft gold,warcraft gold,world of warcraft gold,cheap world of warcraft gold,warcraft gold max(1284)
        
不在的哪天!
final fantasy xi gil,final fantasy xi gil,final fantasy xi gil,final fantasy xi gil, max(5382)
        
昨夜的狂想曲!
wow gold,WoW Gold,world of warcraft gold,WoW Gold, max(3062)
        
没有情人的情人节!
wow gold,wow power leveling.wow power leveling,wow power leveling, max(1532)
        
没有情人的情人节!
wow gold,wow power leveling.wow power leveling,wow power leveling, max(5698)
        
见到你的笑!
maplestory mesos,maplestory mesos,maplestory mesos, maple story mesos, max(1000)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号