- 浏览: 161161 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
beee:
不错,顶啊
删除项目中的所有.svn文件夹(或CVS文件夹) -
Ts_Coo:
java socket编程入门 -
Jiangnan-Xia:
大哥果然相当凑活
删除项目中的所有.svn文件夹(或CVS文件夹) -
yangzhiqi463:
相当凑活
删除项目中的所有.svn文件夹(或CVS文件夹) -
卢水发:
你这东西太给力
删除项目中的所有.svn文件夹(或CVS文件夹)
There're a number of way to do XML serialization in Java. If you want fine-grained control over parsing and serialization you can go for SAX, DOM, or Stax for better performance. Yet, what I often want to do is a simple mapping between POJOs and XML. However, creating Java classes to do XML event parsing manually is not trivial. I recently found JAXB to be a quick and convenient Java-XML mapping or serialization.
JAXB contains a lot of useful features, you can check out the reference implementation here. Kohsuke's blog is also a good resource to learn more about JAXB. For this blog entry, I'll show you how to do a simple Java-XML serialization with JAXB.
POJO to XML
Let's say I have an Item Java object. I want to serialize an Item object to XML format. What I have to do first is to annotate this POJO with a few XML annotation from javax.xml.bind.annotation.* package. See code listing 1 for Item.java
From the code
@XmlRootElement(name="Item") indicates that I want <Item> to be the root element.
@XmlType(propOrder = {"name", "price"}) indicates the order that I want the element to be arranged in XML output.
@XmlAttribute(name="id", ...) indicates that id is an attribute to <Item> root element.
@XmlElement(....) indicates that I want price and name to be element within Item.
My Item.java is ready. I can then go ahead and create JAXB script for marshaling Item.
//creating Item data object
Item item = new Item();
item.setId(2);
item.setName("Foo");
item.setPrice(200);
.....
JAXBContext context = JAXBContext.newInstance(item.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(item, new FileWriter("item.xml")); //I want to save the output file to item.xml
For complete code Listing please see Code Listing 2 (main.java). The output Code Listing 3 item.xml file is created. It looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:item ns1:id="2" xmlns:ns1="http://blogs.sun.com/teera/ns/item">
<ns1:itemName>Foo</ns1:itemName>
<ns1:price>200</ns1:price>
</ns1:item>
Easy right? You can alternatively channel the output XML as text String, Stream, Writer, ContentHandler, etc by simply change the parameter of the marshal(...) method like
...
JAXBContext context = JAXBContext.newInstance(item.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(item, <java.io.OutputStream instance>); // save xml output to the OutputStream instance
...
JAXBContext context = JAXBContext.newInstance(item.getClass());
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(item, sw); //save to StringWriter, you can then call sw.toString() to get java.lang.String
XML to POJO
Let's reverse the process. Assume that I now have a piece of XML string data and I want to turn it into Item.java object. XML data (Code listing 3) looks like
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:item ns1:id="2" xmlns:ns1="http://blogs.sun.com/teera/ns/item">
<ns1:itemName>Bar</ns1:itemName>
<ns1:price>80</ns1:price>
</ns1:item>
I can then unmarshal this xml code to Item object by
...
ByteArrayInputStream xmlContentBytes = new ByteArrayInputStream (xmlContent.getBytes());
JAXBContext context = JAXBContext.newInstance(Item.getClass());
Unmarshaller unmarshaller = context.createUnmarshaller();
//note: setting schema to null will turn validator off
unmarshaller.setSchema(null);
Object xmlObject = Item.getClass().cast(unmarshaller.unmarshal(xmlContentBytes));
return xmlObject;
...
For complete code Listing please see Code Listing 2 (main.java). The XML source can come in many forms both from Stream and file. The only difference, again, is the method parameter:
...
unmarshaller.unmarshal(new File("Item.xml")); // reading from file
...
unmarshaller.unmarshal(inputStream); // inputStream is an instance of java.io.InputStream, reading from stream
Validation with XML Schema
Last thing I want to mention here is validating input XML with schema before unmarshalling to Java object. I create an XML schema file called item.xsd. For complete code Listing please see Code Listing 4 (Item.xsd). Now what I have to do is register this schema for validation.
...
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(new File("Item.xsd"));
unmarshaller.setSchema(schema); //register item.xsd shcema for validation
...
When I try to unmarshal XML data to POJO, if the input XML is not conformed to the schema, exception will be caught. For complete code Listing please see Code Listing 5 (invalid_item.xml).
javax.xml.bind.UnmarshalException
- with linked exception:
javax.xml.bind.JAXBException caught: null
[org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'item1' is not a valid value for 'integer'.]
Here I change the 'id' attribute to string instead of integer.
If XML input is valid against the schema, the XML data will be unmarshalled to Item.java object successfully.
I leave out detail on data encryption and XML digital signature and gazillion of things you can do with JAXB. My goal here is to make the case for JAXB as an alternative way for XML serialization. For more information on JAXB, please check out these links:
- Kohsuke's blog
- Netbeans 6 JAXB tutorial
- Java EE 5 JAXB tutorial
发表评论
-
java keytool证书工具使用小结
2013-12-24 10:05 829Keytool 是一个Java数据证 ... -
Jboss6.0修改端口及windows平台下的安装
2013-12-12 20:04 888jboss下载地址: http://www.jboss.or ... -
java 获取文件的方式
2010-09-29 10:51 958首先,Java中的getResourceAsStream有 ... -
log4j 输出格式详解
2010-09-28 13:34 2025Log4j建议只使用四个级别,优先级从高到低分别是ERRO ... -
Hibernate读写Oracle BLOB
2010-09-26 14:41 1071CREATE TABLE TUSER( ID V ... -
Hibernate的generator属性的意义
2010-09-26 11:06 908本文讲述Hibernate的generator属性的意义。 ... -
Duplicate class/entity mapping错
2010-09-26 10:05 1252是因为hibernate-mapping中没有定义pac ... -
Java字符编码相关
2010-05-20 11:11 5503JVM启动后,JVM会设置一些系统属性以表明JV ... -
svn管理分支(转)
2010-04-21 17:45 1752转自(http://jasonchi.iteye.com/bl ... -
SVN trunk, branches and tag
2010-04-21 17:25 1764翻译者:zwws原 文: SVN trunk, branc ... -
java导入导出excel操作(jxl)
2009-08-26 11:22 1230jxl.jar 包 下载地址: http://w ... -
Log4j配置文件
2009-08-04 15:11 1843b.log4j.properties ,为log4 ... -
Xpath表达式
2009-06-11 16:32 1633XPath 数据类型 XPath 可分为四种数据 ... -
Cron 表达式
2009-06-05 17:16 959Field Name ... -
java 定时任务
2009-06-05 16:42 1358用 Quartz 进行作业调度 ... -
程序形成死锁的四个条件
2009-05-12 15:02 895死锁的条件 互斥条件(Mutual exclusion):资源 ... -
介绍一个调试监测TCP链接的好工具
2009-02-26 10:31 2071最近在写WebService程序和Socket编程,将一个不错 ... -
java虚拟机垃圾回收机制
2009-01-05 12:00 2740一、相关概念 基本回收算法 引用计数(Reference C ... -
java application中内嵌ActiveX控件
2009-01-05 09:53 3439我这里用的是SWT/JFace开发applicati ... -
用脚本运行java GUI程序
2009-01-05 09:50 1897做了一段时间的java GUI编程,现在把一些心得写出来大家共 ...
相关推荐
赠送jar包:jaxb-svg11-1.0.2.jar; 赠送原API文档:jaxb-svg11-1.0.2-javadoc.jar; 赠送源代码:jaxb-svg11-1.0.2-sources.jar; 赠送Maven依赖信息文件:jaxb-svg11-1.0.2.pom; 包含翻译后的API文档:jaxb-svg11...
Maven坐标:javax.xml.bind:jaxb-api:2.3.1; 标签:bind、javax、xml、api、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,...
标题中的"jaxb-impl-2.3.0、jaxb-core-2.3.0、jaxb-api-2.3.0"是JAXB的不同组件版本号,它们在处理XML到Java对象之间的转换时起到关键作用。在描述中提到的“Maven项目中缺少jaxb-api的异常报错”,通常指的是在运行...
赠送jar包:jaxb-core-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-core-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-core-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-core-...
Java Architecture for XML Binding (JAXB) 是Java平台标准的一部分,用于在Java对象和XML文档之间进行数据绑定。它使得开发者可以方便地将Java类转换为XML格式,反之亦然,无需编写大量的转换代码。JAXB提供了高效...
赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...
赠送jar包:jaxb-svg11-1.0.2.jar; 赠送原API文档:jaxb-svg11-1.0.2-javadoc.jar; 赠送源代码:jaxb-svg11-1.0.2-sources.jar; 赠送Maven依赖信息文件:jaxb-svg11-1.0.2.pom; 包含翻译后的API文档:jaxb-svg11...
赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...
`jaxb-api-2.1.jar` 和 `jaxb-impl-2.1.8.jar` 是Java应用程序中用于XML绑定(Java Architecture for XML Binding,简称JAXB)的重要库文件。JAXB是Java SE和Java EE平台的标准部分,它提供了一种将XML文档与Java...
jaxb-core-2.3.0.1.jar
赠送jar包:jaxb-impl-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-impl-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-impl-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-impl-...
JAXB-XML 注解应用 一、JAXB 概念和特点 JAXB(Java Architecture for XML Binding)是一项业界标准,它可以根据 XML Schema 产生 Java 类的技术。该过程中,JAXB 也提供了将 XML 实例文档反向生成 Java 对象树的...
5. **jaxb-xjc.jar**:这个库包含了XJC工具,它是JAXB编译器,可以将XML Schema转换为对应的Java源代码。通过XJC,开发者可以直接从XML Schema生成Java类,从而简化了处理XML数据的工作。 6. **jsr173_1.0_api.jar*...
赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...
总之,JAXB是Java开发者处理XML数据的强大工具,而`jaxb-api.jar`和`jaxb-impl.jar`是其核心组件,提供了完整的API接口和实现。理解并熟练运用这两个库,能有效地提升处理XML数据的效率和质量。
JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类。JAXB允许JAVA人员将JAVA类映射为XML表示方式,...
Java Architecture for XML Binding (JAXB) 是Java平台上的一个标准技术,它允许程序开发者将XML文档与Java对象之间进行绑定,实现XML数据的序列化和反序列化。JAXB是Java SE和Java EE环境中的一部分,提供了高效且...
Java Architecture for XML Binding (JAXB) 是Java平台中用于XML到Java对象绑定的标准技术,它允许开发者在Java程序中直接操作XML数据,而无需编写大量的XML解析和序列化代码。JAXB 2.3.0是该技术的一个版本,包含了...
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-core-2.3.0.jar
Maven坐标:javax.xml.bind:jaxb-api:2.3.0; 标签:javax、xml、bind、jaxb、api、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,...