码迷,mamicode.com
首页 > 编程语言 > 详细

java生成解析xml的另外两种方法JAXB

时间:2017-04-28 13:39:29      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:oschina   mode   wrapper   span   osc   使用   信息   gif   time   

JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。

    JAXBContext 类提供到 JAXB API 的客户端入口点。它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。

 

  在JDK1.6时,JAXB 2.0是JDK 1.6的组成部分。JAXB 2.2.3是JDK 1.7的组成部分。

 

 JDK中JAXB相关的重要Class和Interface:

  1、JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。

  2、Marshaller接口,将Java对象序列化为XML数据。

  3、Unmarshaller接口,将XML数据反序列化为Java对象。

JDK中JAXB相关的重要Annotation:

  1、@XmlType,将Java类或枚举类型映射到XML模式类型

  2、@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标注)字段到XML。其他值还有XmlAccessType.PROPERTY和XmlAccessType.NONE。

  3、@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。

  4、@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML。

  5、@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)。

  6、@XmlRootElement,将Java类或枚举类型映射到XML元素。

  7、@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。

  8、@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。

 

 

技术分享
DataBean.java

package com.xidian.wq.imaopay.model.mesdata.base;
 
import javax.xml.bind.annotation.XmlRootElement;
 
/**
 *
 * @author BYSocket
 * 报文头-[报文基础信息]
 */
@XmlRootElement(name="data_bean")
public class DataBean
{
    // 版本信息(data_version)
    String version;
     
    ////消息类型(data_type)
    // 000001 - 注册[个人信息Post|Respose]
    // 000002 - 绑定银行卡[首次绑定,保存个人信息]
    // 000003 - 绑定银行卡[二次,校验个人信息]
    // 100001 - 支付[钱包支付]
    // 100002 - 查询
    // 200001
    String data_type;
     
    ////订单编号(order_id)
    // 总长20位, 有字母要用大写
    String batch_no;
     
    // 平台账号(platform_account)
    String user_name;
     
    ////请求状态
    // 000
    String trans_state;
     
    // 签名信息(msg_sign)
    String msg_sign;
 
    // 时间戳(randomTime)
    private String rd_time;
 
    // 随机数(randomNum)
    private String rd_num;
 
    // 密钥(keySign)
    private String k_sign;
     
    // 保留域
    String reserve;
 
    public String getVersion()
    {
        return version;
    }
 
    public void setVersion(String version)
    {
        this.version = version;
    }
 
    public String getData_type()
    {
        return data_type;
    }
 
    public void setData_type(String data_type)
    {
        this.data_type = data_type;
    }
 
    public String getBatch_no()
    {
        return batch_no;
    }
 
    public void setBatch_no(String batch_no)
    {
        this.batch_no = batch_no;
    }
 
    public String getUser_name()
    {
        return user_name;
    }
 
    public void setUser_name(String user_name)
    {
        this.user_name = user_name;
    }
 
    public String getTrans_state()
    {
        return trans_state;
    }
 
    public void setTrans_state(String trans_state)
    {
        this.trans_state = trans_state;
    }
 
    public String getMsg_sign()
    {
        return msg_sign;
    }
 
    public void setMsg_sign(String msg_sign)
    {
        this.msg_sign = msg_sign;
    }
 
    public String getRd_time()
    {
        return rd_time;
    }
 
    public void setRd_time(String rd_time)
    {
        this.rd_time = rd_time;
    }
 
    public String getRd_num()
    {
        return rd_num;
    }
 
    public void setRd_num(String rd_num)
    {
        this.rd_num = rd_num;
    }
 
    public String getK_sign()
    {
        return k_sign;
    }
 
    public void setK_sign(String k_sign)
    {
        this.k_sign = k_sign;
    }
 
    public String getReserve()
    {
        return reserve;
    }
 
    public void setReserve(String reserve)
    {
        this.reserve = reserve;
    }
 
}
RegBean.java
package com.xidian.wq.imaopay.model.mesdata.base;
 
 
import javax.xml.bind.annotation.XmlRootElement;
 
/**
 *
 * @author BYSocket
 * 报文体-[用户注册报文体信息]
 */
@XmlRootElement(name="reg_bean")
public class RegBean
{
     
    // 注册报文体流水号(reg_data_sn)
    private String reg_sn;
     
    ////用户编号(user_info_id)
    // 业务系统的用户编号只做记录不在支付系统中逻辑处理
    private Integer user_id;
 
    // 注册账号(reg_account)
    private String reg_no;
     
    // 注册途径(reg_way)
    private String reg_way;
     
    // 保留域
    private String reserve;
     
    ////提交日期(set_time)
    // 业务系统上传
    private String set_time;
     
    // 后台信息返回路径(return_url)
    private String ret_url;
     
    // 备注
    String remarks;
 
    public String getReg_sn()
    {
        return reg_sn;
    }
 
    public void setReg_sn(String reg_sn)
    {
        this.reg_sn = reg_sn;
    }
 
    public Integer getUser_id()
    {
        return user_id;
    }
 
    public void setUser_id(Integer user_id)
    {
        this.user_id = user_id;
    }
 
    public String getReg_no()
    {
        return reg_no;
    }
 
    public void setReg_no(String reg_no)
    {
        this.reg_no = reg_no;
    }
 
    public String getReg_way()
    {
        return reg_way;
    }
 
    public void setReg_way(String reg_way)
    {
        this.reg_way = reg_way;
    }
 
    public String getReserve()
    {
        return reserve;
    }
 
    public void setReserve(String reserve)
    {
        this.reserve = reserve;
    }
 
    public String getSet_time()
    {
        return set_time;
    }
 
    public void setSet_time(String set_time)
    {
        this.set_time = set_time;
    }
 
    public String getRet_url()
    {
        return ret_url;
    }
 
    public void setRet_url(String ret_url)
    {
        this.ret_url = ret_url;
    }
 
    public String getRemarks()
    {
        return remarks;
    }
 
    public void setRemarks(String remarks)
    {
        this.remarks = remarks;
    }
 
}


注册报文体MsgRegBean.java,支持内容的列表。

package com.xidian.wq.imaopay.model.mesdata;
 
import java.util.List;
 
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
 
import com.xidian.wq.imaopay.model.mesdata.base.DataBean;
import com.xidian.wq.imaopay.model.mesdata.base.RegBean;
 
@XmlRootElement( name = "msg_bean" )
public class MsgRegBean
{
    private DataBean dataBean;
     
    private List<RegBean> regBeans;
 
     
    public DataBean getDataBean()
    {
        return dataBean;
    }
 
    @XmlElement(name = "data_bean")
    public void setDataBean(DataBean dataBean)
    {
        this.dataBean = dataBean;
    }
 
    @XmlElementWrapper(name = "reg_beans") 
    @XmlElement(name = "reg_bean")
    public List<RegBean> getRegBeans()
    {
        return regBeans;
    }
 
    public void setRegBeans(List<RegBean> regBeans)
    {
        this.regBeans = regBeans;
    }
 
}
然后针对的我们用Jaxb实现一个转换的Util类:
package com.xidian.wq.imaopay.util;
import java.io.StringReader; 
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 
 
import com.alibaba.fastjson.JSON;
import com.xidian.wq.imaopay.model.mesdata.MsgRegBean;
import com.xidian.wq.imaopay.model.mesdata.base.DataBean;
import com.xidian.wq.imaopay.model.mesdata.base.RegBean;
 
/**
 *  
 * @author BYSocket
 * Jaxb2.0 处理Xml与Object转换
 *
 */
public class JaxbObjectAndXmlUtil
{ 
       
    /**
     * @param xmlStr 字符串
     * @param c 对象Class类型
     * @return 对象实例
     */
    @SuppressWarnings("unchecked")
    public static <T> T xml2Object(String xmlStr,Class<T> c)
    { 
        try
        { 
            JAXBContext context = JAXBContext.newInstance(c); 
            Unmarshaller unmarshaller = context.createUnmarshaller(); 
             
            T t = (T) unmarshaller.unmarshal(new StringReader(xmlStr)); 
             
            return t; 
             
        } catch (JAXBException e) {  e.printStackTrace();  return null; } 
         
    } 
       
    /**
     * @param object 对象
     * @return 返回xmlStr
     */
    public static String object2Xml(Object object)
    { 
        try
        {   
            StringWriter writer = new StringWriter();
            JAXBContext context = JAXBContext.newInstance(object.getClass()); 
            Marshaller  marshal = context.createMarshaller();
             
            marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化输出 
            marshal.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式,默认为utf-8 
            marshal.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xml头信息 
            marshal.setProperty("jaxb.encoding", "utf-8"); 
            marshal.marshal(object,writer);
             
            return new String(writer.getBuffer());
             
        } catch (Exception e) { e.printStackTrace(); return null;}    
         
    } 
     
    public static void main(String[] args)
    {
         /** 构造测试报文头对象 */
        DataBean dataBean = new DataBean();
        dataBean.setBatch_no("N20150204");
        dataBean.setData_type("000001");
        dataBean.setVersion("v1.0");
        dataBean.setUser_name("xx");
        dataBean.setMsg_sign("未知");
        dataBean.setRd_num("6631383");
        dataBean.setRd_time("20150204");
        dataBean.setK_sign("75CC479AAC09F00BA28F0E968B1BC9D1B90ADCC2");
 
        /** 构造测试报文体对象 */
        RegBean regBean = new RegBean();
        regBean.setReg_sn("REG20150204");
        regBean.setUser_id(12);
        regBean.setReg_no("33");
        regBean.setReg_way("pc");
        regBean.setSet_time("20150204 16:18");
        regBean.setRet_url("未知");
        regBean.setRemarks("无备注");
         
        MsgRegBean msgBean = new MsgRegBean();
        List<RegBean> regBeans = new ArrayList<RegBean>();
        regBeans.add(regBean);
        regBeans.add(regBean);
         
        msgBean.setRegBeans(regBeans);
        msgBean.setDataBean(dataBean);
         
        String xmlStr = JaxbObjectAndXmlUtil.object2Xml(msgBean);//构造报文 XML 格式的字符串
        System.out.println("对象转xml报文: \n"+xmlStr);
         
        MsgRegBean msgBean2 = JaxbObjectAndXmlUtil.xml2Object(xmlStr, MsgRegBean.class);
        System.out.println("报文转xml转: \n"+JSON.toJSONString(msgBean2));
    }
}  </font>

运行下测试main函数,可以得到如下的输出:

对象转xml报文:
<?xmlversion="1.0"encoding="utf-8"standalone="yes"?>
<msg_bean>
    <data_bean>
        <batch_no>N20150204</batch_no>
        <data_type>000001</data_type>
        <k_sign>75CC479AAC09F00BA28F0E968B1BC9D1B90ADCC2</k_sign>
        <msg_sign>未知</msg_sign>
        <rd_num>6631383</rd_num>
        <rd_time>20150204</rd_time>
        <user_name>xx</user_name>
        <version>v1.0</version>
    </data_bean>
    <reg_beans>
        <reg_bean>
            <reg_no>33</reg_no>
            <reg_sn>REG20150204</reg_sn>
            <reg_way>pc</reg_way>
            <remarks>无备注</remarks>
            <ret_url>未知</ret_url>
            <set_time>20150204 16:18</set_time>
            <user_id>12</user_id>
        </reg_bean>
        <reg_bean>
            <reg_no>33</reg_no>
            <reg_sn>REG20150204</reg_sn>
            <reg_way>pc</reg_way>
            <remarks>无备注</remarks>
            <ret_url>未知</ret_url>
            <set_time>20150204 16:18</set_time>
            <user_id>12</user_id>
        </reg_bean>
    </reg_beans>
</msg_bean>
 
报文转xml转:
{"dataBean":{"batch_no":"N20150204","data_type":"000001","k_sign":"75CC479AAC09F00BA28F0E968B1BC9D1B90ADCC2","msg_sign":"未知","rd_num":"6631383","rd_time":"20150204","user_name":"xx","version":"v1.0"},"regBeans":[{"reg_no":"33","reg_sn":"REG20150204","reg_way":"pc","remarks":"无备注","ret_url":"未知","set_time":"20150204 16:18","user_id":12},{"reg_no":"33","reg_sn":"REG20150204","reg_way":"pc","remarks":"无备注","ret_url":"未知","set_time":"20150204 16:18","user_id":12}]}
代码中间注意点:
  Marshaller.JAXB_FORMATTED_OUTPUT :决定是否在转换成xml时同时进行格式化(即按标签自动换行,否则即是一行的xml) 。

  Marshaller.JAXB_ENCODING :xml的编码方式。

  Marshaller.JAXB_FRAGMENT :是否省略xml头信息

  marshal.setProperty(“jaxb.encoding”, “utf-8″); : 设置编码格式 utf-8
技术分享

转自:http://my.oschina.net/jeffli1993/blog/376846

java生成解析xml的另外两种方法JAXB

标签:oschina   mode   wrapper   span   osc   使用   信息   gif   time   

原文地址:http://www.cnblogs.com/lqCnblog/p/6781049.html

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