| |
| 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) |
|
|
| |
| 免责声明:该文章由网友发表,如果对您造成侵权,请联系站长。 |
|