码迷,mamicode.com
首页 > 其他好文 > 详细

Jaxb笔记

时间:2014-06-21 10:54:32      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   java   

摘自: http://www.blogjava.net/eagle-daiq/archive/2012/01/30/369016.html

最近项目原因,研究了下jaxbjaxbJava api xml binding的简称,是为实现javaxml数据的相互转换而定义的一个api标准。该标准以annotation的方式实现xml的转换。不用开发人员单独解析每个对象属性与xml元素的mapping关系,只需在java bean中注入简单的java annotation,其他的交给工具去处理。该工具包类能给xml数据处理带来极大方便。具体实现见下。

Java bean对象定义:

bubuko.com,布布扣/**
bubuko.com,布布扣 * 促销xml对象类 
bubuko.com,布布扣 * 
@author daiqiang
bubuko.com,布布扣 * 对应xml文件内容如下:
bubuko.com,布布扣 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
bubuko.com,布布扣    <promotion>
bubuko.com,布布扣        <id>promotionId</id>
bubuko.com,布布扣        <name>元旦促销</name>
bubuko.com,布布扣        <type>CMS</type>
bubuko.com,布布扣        <typeDes>CMS主推促销</typeDes>
bubuko.com,布布扣        <startTime>2012-01-01</startTime>
bubuko.com,布布扣        <endTime>2012-01-03</endTime>
bubuko.com,布布扣        <products>
bubuko.com,布布扣            <product>
bubuko.com,布布扣                <merchantId>merchantid</merchantId>
bubuko.com,布布扣                <num>500</num>
bubuko.com,布布扣                <productCode>code1</productCode>
bubuko.com,布布扣                <productId>111</productId>
bubuko.com,布布扣                <requestId>codedata</requestId>
bubuko.com,布布扣            </product>
bubuko.com,布布扣            <product>
bubuko.com,布布扣                <merchantId>merchantid2</merchantId>
bubuko.com,布布扣                <num>800</num>
bubuko.com,布布扣                <productCode>code2</productCode>
bubuko.com,布布扣                <productId>2</productId>
bubuko.com,布布扣                <requestId>codedata</requestId>
bubuko.com,布布扣            </product>
bubuko.com,布布扣        </products>
bubuko.com,布布扣    </promotion>
bubuko.com,布布扣 *
bubuko.com,布布扣 
*/

bubuko.com,布布扣@XmlRootElement(name
="promotion")
bubuko.com,布布扣@XmlAccessorType(XmlAccessType.FIELD)
bubuko.com,布布扣
public class Promotion implements Serializable{
bubuko.com,布布扣
bubuko.com,布布扣    
private static final long serialVersionUID = 870036805093867083L;
bubuko.com,布布扣    
bubuko.com,布布扣    
private String id;
bubuko.com,布布扣    
private String name;
bubuko.com,布布扣    
private String type;
bubuko.com,布布扣    
private String typeDes;
bubuko.com,布布扣    
private String startTime;
bubuko.com,布布扣    
private String endTime;
bubuko.com,布布扣    
bubuko.com,布布扣    @XmlElementWrapper(name
="products")
bubuko.com,布布扣    @XmlElement(name
="product")
bubuko.com,布布扣    
private List<Product> products;
bubuko.com,布布扣    
bubuko.com,布布扣    
/*@XmlTransient
bubuko.com,布布扣    the field is not binded to xml
bubuko.com,布布扣    private String testHiddenFields;
*/

bubuko.com,布布扣    
//此处省略具体set get 方法。
bubuko.com,布布扣

 

说明:上文定义了一个促销对象类Promotion.

类标注表示:

@XmlRootElement:用于定义该对象映射成xml根节点元素名,默认与类名一致。可通过@XmlRootElement(name="otherRootElement")方式指定具体名称。

 

@XmlAccessorType: 用于标识该java对象与xml映射的访问方式。有如下属性值。

PROPERTY/FIELD/PUBLIC_MEMBER/NONE

 

PROPERTY: 所有set/get方法对将被映射为xml元素.除非被XmlTransient标注例外.

 

FIELD:所有对象属性将被映射为xml元素。除非被XmlTransient标注例外.

 

PUBLIC_MEMBER每个publicget/set对方法或public field将被映射为xml元素。除非被XmlTransient标注例外.

 

NONE没有fields property被映射,除非显示指定具体fieldsproperty

 

 

属性标注表示:

@XmlTransient:指对应属性不做xml映射。

@XmlElement(name="product"):指定属性映射时对应xml元素名称

@XmlElementWrapper(name="products"):在某些场景下,需要对映射的属性做包装处理。如例子中products List对象属性,在xml中我想在映射对所有的product元素再做一个products 元素包装,如下所示,就可以按此种方式实现。

<products>

    <product> … </product>

    <product> … </product>

    …

</products>

 

Javaxml映射方法

 

Java对象到XML

 

bubuko.com,布布扣/**
bubuko.com,布布扣     * convent java object to xml format String.
bubuko.com,布布扣     * 
bubuko.com,布布扣     * 
@param originalObj
bubuko.com,布布扣     * 
@param xmlCharset
bubuko.com,布布扣     *            the format of charset for xml. ie "UTF-8", "GBK"
bubuko.com,布布扣     * 
@param isFragment
bubuko.com,布布扣     *            whether or not display the header for the generated xml. such
bubuko.com,布布扣     *            as <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
bubuko.com,布布扣     * 
@return
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static String convertJava2XmlStr(Object originalObj,
bubuko.com,布布扣            String xmlCharset, 
boolean isFragment) {
bubuko.com,布布扣        String xmlStr 
= "";
bubuko.com,布布扣        
try {
bubuko.com,布布扣            JAXBContext ctx 
= JAXBContext.newInstance(originalObj.getClass());
bubuko.com,布布扣            Marshaller marshaller 
= ctx.createMarshaller();
bubuko.com,布布扣            marshaller.setProperty(Marshaller.JAXB_ENCODING, xmlCharset);
bubuko.com,布布扣            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, 
true);
bubuko.com,布布扣            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, isFragment);
bubuko.com,布布扣
bubuko.com,布布扣            ByteArrayOutputStream os 
= new ByteArrayOutputStream();
bubuko.com,布布扣            marshaller.marshal(originalObj, os);
bubuko.com,布布扣
bubuko.com,布布扣            xmlStr 
= os.toString();
bubuko.com,布布扣        }
 catch (PropertyException e) {
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣        }
 catch (JAXBException e) {
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣        }
 catch (Exception e) {
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣        }

bubuko.com,布布扣        
return xmlStr;
bubuko.com,布布扣    }

bubuko.com,布布扣

 

XMLJava对象

 

bubuko.com,布布扣/**
bubuko.com,布布扣     * convert xml string to Java object by JAXB.
bubuko.com,布布扣     * 
@param obj  to convert java object.
bubuko.com,布布扣     * 
@param xmlStr    
bubuko.com,布布扣     * 
@return
bubuko.com,布布扣     
*/

bubuko.com,布布扣    
public static Object convertXmlStr2Java(Object obj, String xmlStr) {
bubuko.com,布布扣        
try {
bubuko.com,布布扣            JAXBContext ctx 
= JAXBContext.newInstance(obj.getClass());
bubuko.com,布布扣            InputStream source 
= new ByteArrayInputStream(xmlStr.getBytes());
bubuko.com,布布扣            Unmarshaller unmarshaller 
= ctx.createUnmarshaller();
bubuko.com,布布扣            obj 
= unmarshaller.unmarshal(source);
bubuko.com,布布扣        }
 catch (JAXBException e) {
bubuko.com,布布扣            e.printStackTrace();
bubuko.com,布布扣        }

bubuko.com,布布扣        
return obj;
bubuko.com,布布扣    }

bubuko.com,布布扣





Jaxb笔记,布布扣,bubuko.com

Jaxb笔记

标签:des   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/redcoatjk/p/3795085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!