最近项目原因,研究了下jaxb。jaxb是Java api xml binding的简称,是为实现java与xml数据的相互转换而定义的一个api标准。该标准以annotation的方式实现xml的转换。不用开发人员单独解析每个对象属性与xml元素的mapping关系,只需在java bean中注入简单的java annotation,其他的交给工具去处理。该工具包类能给xml数据处理带来极大方便。具体实现见下。
Java bean对象定义:
* 促销xml对象类
* @author daiqiang
* 对应xml文件内容如下:
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<promotion>
<id>promotionId</id>
<name>元旦促销</name>
<type>CMS</type>
<typeDes>CMS主推促销</typeDes>
<startTime>2012-01-01</startTime>
<endTime>2012-01-03</endTime>
<products>
<product>
<merchantId>merchantid</merchantId>
<num>500</num>
<productCode>code1</productCode>
<productId>111</productId>
<requestId>codedata</requestId>
</product>
<product>
<merchantId>merchantid2</merchantId>
<num>800</num>
<productCode>code2</productCode>
<productId>2</productId>
<requestId>codedata</requestId>
</product>
</products>
</promotion>
*
*/
@XmlRootElement(name="promotion")
@XmlAccessorType(XmlAccessType.FIELD)
publicclass Promotion implements Serializable{
privatestaticfinallong serialVersionUID =870036805093867083L;
private String id;
private String name;
private String type;
private String typeDes;
private String startTime;
private String endTime;
@XmlElementWrapper(name="products")
@XmlElement(name="product")
private List<Product> products;
/*@XmlTransient
the field is not binded to xml
private String testHiddenFields;*/
//此处省略具体set get 方法。
说明:上文定义了一个促销对象类Promotion.
类标注表示:
@XmlRootElement:用于定义该对象映射成xml根节点元素名,默认与类名一致。可通过@XmlRootElement(name="otherRootElement")方式指定具体名称。
@XmlAccessorType: 用于标识该java对象与xml映射的访问方式。有如下属性值。
PROPERTY/FIELD/PUBLIC_MEMBER/NONE
PROPERTY: 所有set/get方法对将被映射为xml元素.除非被XmlTransient标注例外.
FIELD:所有对象属性将被映射为xml元素。除非被XmlTransient标注例外.
PUBLIC_MEMBER:每个public的get/set对方法或public field将被映射为xml元素。除非被XmlTransient标注例外.
NONE:没有fields 或 property被映射,除非显示指定具体fields或property。
属性标注表示:
@XmlTransient:指对应属性不做xml映射。
@XmlElement(name="product"):指定属性映射时对应xml元素名称
@XmlElementWrapper(name="products"):在某些场景下,需要对映射的属性做包装处理。如例子中products List对象属性,在xml中我想在映射对所有的product元素再做一个products 元素包装,如下所示,就可以按此种方式实现。
<products>
<product> … </product>
<product> … </product>
…
</products>
Java与xml映射方法
Java对象到XML
* convent java object to xml format String.
*
* @param originalObj
* @param xmlCharset
* the format of charset for xml. ie "UTF-8", "GBK"
* @param isFragment
* whether or not display the header for the generated xml. such
* as <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
* @return
*/
publicstatic String convertJava2XmlStr(Object originalObj,
String xmlCharset, boolean isFragment) {
String xmlStr ="";
try{
JAXBContext ctx = JAXBContext.newInstance(originalObj.getClass());
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, xmlCharset);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, isFragment);
ByteArrayOutputStream os =new ByteArrayOutputStream();
marshaller.marshal(originalObj, os);
xmlStr = os.toString();
}catch (PropertyException e) {
e.printStackTrace();
}catch (JAXBException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return xmlStr;
}
XML到Java对象
* convert xml string to Java object by JAXB.
* @param obj to convert java object.
* @param xmlStr
* @return
*/
publicstatic Object convertXmlStr2Java(Object obj, String xmlStr) {
try{
JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
InputStream source =new ByteArrayInputStream(xmlStr.getBytes());
Unmarshaller unmarshaller = ctx.createUnmarshaller();
obj = unmarshaller.unmarshal(source);
}catch (JAXBException e) {
e.printStackTrace();
}
return obj;
}
相关推荐
JAXB学习笔记 项目为了进行xml与java bean的互转,参考了各位网友的推荐,最后选型为JAXB,闲暇之余整理了一下笔记。废话不多说,上代码
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
jaxb-api jaxb-impl jar
JAXB的安装包及插件
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
NULL 博文链接:https://kamuikyo.iteye.com/blog/1182316
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
使用jaxb生成XML例子,含有例子和注解解析
赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.2.3-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.2.3-sources.jar; 赠送Maven依赖...
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包
现在很多maven仓库均没有此jar jaxb-api-2.3.0.jar jaxb-core-2.3.0.jar
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
jaxb-2.2.jar jaxb jax
赠送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依赖...
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-core-2.3.0.jar
我刚写好的 一个简单的小程序 动行里面的read文件 是在idea 中用jaxb 读xml中数据