jaxb xml to bean


package www.garbin.com.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * Created by linjiabin on 14/11/27.
 */
public class XMLUtils {
    public static <T> T xml2Bean(Class<T> bean, String xmlStr) {
        T s = null;
        try {
            JAXBContext context = JAXBContext.newInstance(bean);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            s = (T)unmarshaller.unmarshal(new StringReader(xmlStr));
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return s;
    }

    public static <T> String bean2Xml(T bean) {
        String xmlStr = null;
        StringWriter writer = null;
        try {
            JAXBContext context = JAXBContext.newInstance(bean.getClass());
            Marshaller marshaller = context.createMarshaller();
            writer = new StringWriter();
            marshaller.marshal(bean, writer);
            xmlStr = writer.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        } finally {
            if (null != writer) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return xmlStr;
    }

}
package www.garbin.com.pojo;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Created by linjiabin on 14/11/28.
 */
@XmlRootElement(name = "Request")
public class Request {
    private String service;
    private String lang;
    private String head;
    private Body body;
    @XmlAttribute(name = "service")
    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }
    @XmlAttribute(name = "lang")
    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }
    @XmlElement(name = "Head")
    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }
    @XmlElement(name = "Body")
    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }

    public static class Body {
        private Order order;
        @XmlElement(name = "Order")
        public Order getOrder() {
            return order;
        }

        public void setOrder(Order order) {
            this.order = order;
        }
    }
    public static class Order {
        private String orderId;
        private String expressType;
        private String jCompany;
        private String jContact;
        private String jTel;
        private String jAddress;
        private String dCompany;
        private String dContact;
        private String dTel;
        private String dAddress;
        private Integer parcelQuantity;
        private String payMethod;
        private OrderOption orderOption;

        private Extra extra;
        @XmlAttribute(name = "orderid")
        public String getOrderId() {
            return orderId;
        }
        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }
        @XmlAttribute(name = "express_type")
        public String getExpressType() {
            return expressType;
        }
        public void setExpressType(String expressType) {
            this.expressType = expressType;
        }
        @XmlAttribute(name = "getj_company")
        public String getjCompany() {
            return jCompany;
        }
        public void setjCompany(String jCompany) {
            this.jCompany = jCompany;
        }
        @XmlAttribute(name = "j_contact")
        public String getjContact() {
            return jContact;
        }
        public void setjContact(String jContact) {
            this.jContact = jContact;
        }
        @XmlAttribute(name = "j_tel")
        public String getjTel() {
            return jTel;
        }
        public void setjTel(String jTel) {
            this.jTel = jTel;
        }
        @XmlAttribute(name = "j_address")
        public String getjAddress() {
            return jAddress;
        }
        public void setjAddress(String jAddress) {
            this.jAddress = jAddress;
        }
        @XmlAttribute(name = "j_company")
        public String getdCompany() {
            return dCompany;
        }
        public void setdCompany(String dCompany) {
            this.dCompany = dCompany;
        }
        @XmlAttribute(name = "d_contact")
        public String getdContact() {
            return dContact;
        }
        public void setdContact(String dContact) {
            this.dContact = dContact;
        }
        @XmlAttribute(name = "d_tel")
        public String getdTel() {
            return dTel;
        }
        public void setdTel(String dTel) {
            this.dTel = dTel;
        }
        @XmlAttribute(name = "d_address")
        public String getdAddress() {
            return dAddress;
        }
        public void setdAddress(String dAddress) {
            this.dAddress = dAddress;
        }
        @XmlAttribute(name = "parcel_quantity")
        public Integer getParcelQuantity() {
            return parcelQuantity;
        }
        public void setParcelQuantity(Integer parcelQuantity) {
            this.parcelQuantity = parcelQuantity;
        }
        @XmlAttribute(name = "pay_method")
        public String getPayMethod() {
            return payMethod;
        }
        public void setPayMethod(String payMethod) {
            this.payMethod = payMethod;
        }
        @XmlElement(name = "OrderOption")
        public OrderOption getOrderOption() {
            return orderOption;
        }
        public void setOrderOption(OrderOption orderOption) {
            this.orderOption = orderOption;
        }

        @XmlElement(name = "Extra")
        public Extra getExtra() {
            return extra;
        }
        public void setExtra(Extra extra) {
            this.extra = extra;
        }
    }
    public static class OrderOption {
        private String custId;
        private String jShippercode;
        private String dDeliverycode;
        private String cargo;
        private Float cargoTotalWeight;
        private String sendstarttime;
        private String mailno;
        private String remark;
        private AddedService addedService;
        @XmlAttribute(name = "custid")
        public String getCustId() {
            return custId;
        }

        public void setCustId(String custId) {
            this.custId = custId;
        }
        @XmlAttribute(name = "j_shippercode")
        public String getjShippercode() {
            return jShippercode;
        }

        public void setjShippercode(String jShippercode) {
            this.jShippercode = jShippercode;
        }
        @XmlAttribute(name = "d_deliverycode")
        public String getdDeliverycode() {
            return dDeliverycode;
        }

        public void setdDeliverycode(String dDeliverycode) {
            this.dDeliverycode = dDeliverycode;
        }
        @XmlAttribute(name = "cargo")
        public String getCargo() {
            return cargo;
        }

        public void setCargo(String cargo) {
            this.cargo = cargo;
        }
        @XmlAttribute(name = "cargo_total_weight")
        public Float getCargoTotalWeight() {
            return cargoTotalWeight;
        }

        public void setCargoTotalWeight(Float cargoTotalWeight) {
            this.cargoTotalWeight = cargoTotalWeight;
        }
        @XmlAttribute(name = "sendstarttime")
        public String getSendstarttime() {
            return sendstarttime;
        }

        public void setSendstarttime(String sendstarttime) {
            this.sendstarttime = sendstarttime;
        }
        @XmlAttribute(name = "mailno")
        public String getMailno() {
            return mailno;
        }

        public void setMailno(String mailno) {
            this.mailno = mailno;
        }
        @XmlAttribute(name = "remark")
        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
        @XmlElement(name = "AddedService")
        public AddedService getAddedService() {
            return addedService;
        }
        public void setAddedService(AddedService addedService) {
            this.addedService = addedService;
        }
    }
    public static class AddedService {
        private String name;
        private String value;
        private String value1;
        private String value2;
        private String value3;
        @XmlAttribute(name = "name")
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
        @XmlAttribute(name = "value")
        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
        @XmlAttribute(name = "value1")
        public String getValue1() {
            return value1;
        }

        public void setValue1(String value1) {
            this.value1 = value1;
        }
        @XmlAttribute(name = "value2")
        public String getValue2() {
            return value2;
        }

        public void setValue2(String value2) {
            this.value2 = value2;
        }
        @XmlAttribute(name = "value3")
        public String getValue3() {
            return value3;
        }

        public void setValue3(String value3) {
            this.value3 = value3;
        }
    }
    public static class Extra {
        private String e1;
        @XmlAttribute(name = "e1")
        public String getE1() {
            return e1;
        }

        public void setE1(String e1) {
            this.e1 = e1;
        }
    }

}
package www.garbin.com.pojo;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import java.io.Serializable;

/**
 * Created by linjiabin on 14/11/28.
 */
@XmlRootElement(name = "Response")
public class Response implements Serializable {
    private String service;
    private String head;
    private Error error;
    private Body body;
    @XmlAttribute(name = "service")
    public String getService() {
        return service;
    }
    public void setService(String service) {
        this.service = service;
    }
    @XmlElement(name = "Head")
    public String getHead() {
        return head;
    }
    public void setHead(String head) {
        this.head = head;
    }
    @XmlElement(name = "ERROR")
    public Error getError() {
        return error;
    }

    public void setError(Error error) {
        this.error = error;
    }
    @XmlElement(name = "Body")
    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }
    public static class Error {
        private String value;
        private String code;
        @XmlValue
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        @XmlAttribute(name = "code")
        public String getCode() {
            return code;
        }
        public void setCode(String code) {
            this.code = code;
        }
    }
    public static class Body {
        private OrderResponse orderResponse;
        @XmlElement(name = "OrderResponse")
        public OrderResponse getOrderResponse() {
            return orderResponse;
        }
        public void setOrderResponse(OrderResponse orderResponse) {
            this.orderResponse = orderResponse;
        }
    }
    public static class OrderResponse {
        private String orderId;
        private String mailno;
        private String origincode;
        private String destcode;
        private String filterResut;
        private String remark;
        @XmlAttribute(name = "orderid")
        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }
        @XmlAttribute(name = "mailno")
        public String getMailno() {
            return mailno;
        }

        public void setMailno(String mailno) {
            this.mailno = mailno;
        }
        @XmlAttribute(name = "origincode")
        public String getOrigincode() {
            return origincode;
        }

        public void setOrigincode(String origincode) {
            this.origincode = origincode;
        }
        @XmlAttribute(name = "destcode")
        public String getDestcode() {
            return destcode;
        }

        public void setDestcode(String destcode) {
            this.destcode = destcode;
        }
        @XmlAttribute(name = "filter_result")
        public String getFilterResut() {
            return filterResut;
        }

        public void setFilterResut(String filterResut) {
            this.filterResut = filterResut;
        }
        @XmlAttribute(name = "remark")
        public String getRemark() {
            return remark;
        }
        public void setRemark(String remark) {
            this.remark = remark;
        }
    }

}
package www.garbin.com.test;

import www.garbin.com.pojo.Request;
import www.garbin.com.utils.XMLUtils;

import javax.xml.bind.JAXBException;
import java.rmi.RemoteException;
import java.util.Random;

/**
 * Created by linjiabin on 14/11/28.
 */
public class TestXMl {
    public static void main(String[] args) throws JAXBException, RemoteException {
        Request request = new Request();
        request.setLang("zh-CN");
        request.setService("OrderService");
        Request.Body body = new Request.Body();
        Request.Order requestOrder = new Request.Order();
        requestOrder.setOrderId("WSD_22222232" + "_" + new Random().nextInt());
        requestOrder.setdAddress("福建厦门市思明区软件园二期望海路55号");
        requestOrder.setjAddress("福建厦门市思明区软件园二期望海路55号");
        requestOrder.setdTel("13506962224");
        requestOrder.setjTel("13514087953");
        requestOrder.setExpressType("2");
        requestOrder.setParcelQuantity(1);
        requestOrder.setPayMethod("1");
        Request.OrderOption orderOption = new Request.OrderOption();
        orderOption.setCustId("5555555555");
        Request.AddedService addedService = new Request.AddedService();
        addedService.setName("COD");
        addedService.setValue("1111");
        addedService.setValue1("5555555555");
        orderOption.setAddedService(addedService);
        requestOrder.setOrderOption(orderOption);
        body.setOrder(requestOrder);
        request.setBody(body);
        request.setHead("SHFJ,Eta7siUxZX5zIfNO");
        String xmlStr = XMLUtils.bean2Xml(request);
        System.out.println(xmlStr);
    }
}
时间: 2024-12-07 10:40:52

jaxb xml to bean的相关文章

[转]hibernate在eclipse的逆向工程生成hbm.xml和bean类

原文地址:http://www.xuebuyuan.com/210489.html 以前一直用myelipse,在myeclipse做hibernate逆向工程倒是很顺手了. 可是最近改用eclipse了,开始是不会装eclipse插件,折腾了几天终于装下去了,可是又不会用病逆向工程 终于在一国外网站上终于找到了: http://iablog.sybase.com/paulley/2009/02/reverse-engineering-sql-anywhere-with-hibernate-to

JAXB XML到java object的转换

JAXB是Java Architecture for XML Binding的缩写.使用JAXB注解将Java对象转换成XML文件.在这篇教程中,我们将会展示如何使用JAXB来做以下事情: 1. marshall 将java对象转化成xml文件 2. unmarshalling 将xml内容转换成java对象 JAXB 注解(Annotation) 如果一个对象需要被转换成XML文件,或者从XML文件中生成,该对象需要用JAXB注解来标注.这些注解光凭名字就知道是什么意思了.具体可参考官网:ja

第2章—装配Bean—通过XML装配Bean

通过XML装配Bean ? 尽管我们在生成Bean的过程中可以用到很多方法,但我们依然需要Spring的XML配置来完善更多的需求,下面就来介绍下XML装配Bean的过程是怎样的. 3.1创建XML配置规范 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&quo

XStream进行xml和bean互转

加入pom <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.3</version> </dependency> 最简单的使用方式 XStream xStream = new XStream(); //声明XStream注解来源 xStream.proces

spring学习总结——装配Bean学习三(xml装配bean)

通过XML装配bean Spring现在有了强大的自动化配置和基于Java的配置,XML不应该再是你的第一选择了.不过,鉴于已经存在那么多基于XML的Spring配置,所以理解如何在Spring中使用XML还是很重要的.但是,我希望本节的内容只是用来帮助你维护已有的XML配置,在完成新的Spring工作时,希望你会使用自动化配置和JavaConfig. 一.创建XML配置规范 在使用XML为Spring装配bean之前,你需要创建一个新的配置规范.在使用JavaConfig的时候,这意味着要创建

xml和bean之间转化

xml和bean之间转化//bean@XmlRootElement(name = "register")br/>@XmlAccessorType(XmlAccessType.PROPERTY)@XmlType(propOrder = {"name","age" })public class User {private String name;private String age;public String getName() {return

[CXF REST标准实战系列] 一、JAXB xml与javaBean的转换

Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.不认识到犯错,然后得到永久的教训. 2.认识JAXB 3.代码实战 1.不认识到犯错,然后得到永久的教训. 也不是所谓的教训吧,真正的教训来自于对错误的剖析理解很深刻.然后有种"吃一堑,长一智"的感觉才叫教训.近日和团队工头们用CXF3.0和Spring4.0开发一个平台,模仿着第三方支付,用xml进行数据交互

Dom4J XML转bean

package com.baiwang.bop.utils; import com.baiwang.bop.client.BopException; import org.dom4j.Element; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * Created by ChengLuchao on 2

Xstream util xml 与 bean之间互转

package com.demo.util; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.mapper.MapperWrapper; public class XmlUtil { /** * java 转换成xml * @Title: toXml * @Description: TODO * @param obj 对象实例 * @return String xml字符串 */ public st