首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

由xml资料动态生成Java类

2012-12-19 
由xml文件动态生成Java类JAXB(Java Architecture for XML Binding)是一种特殊的序列化/反序列化工具。它可

由xml文件动态生成Java类
      JAXB(Java Architecture for XML Binding)是一种特殊的序列化/反序列化工具。它可以使XML数据以Java Objects的形式直接应用于Java程序之中,使Java Objects与XML数据之间的转换成为可能。在JAXB中将Java Objects到XML数据的转换称为marshal;XML数据到Java Objects的转换称为unmarshal。
      使用JAXB分两步,首先利用xjc命令行程序编译DTD或Schema文件生成与XML数据结构相对应的JavaBeans类,再通过JAXB API中的marshal与unmarshal操作完成JavaBeans对象与XML数据之间的互换。
      JAXB使用了最新的元数据技术,故而只能在JDK 1.5以上版本使用,JAXB的官方网站如下:
https://jaxb.dev.java.net/
1. 通过命令行编译
命令行程序xjc用于编译DTD或Schema生成与XML数据结构对应的JavaBeans类。操作方法如下:


这个目标期望可以生成的类有:ShippingOder,GoodsInformation,ConsigneeInformation和ConsignorInformation
但测试生成的类有除了上面那四个外,还有三个:package-info,ObjectFactory,ArrayOfGoodsInfomation
如ShippingOrder这个类中部分代码如下:
public class ShippingOrder {    @XmlSchemaType(name = "dateTime")    protected XMLGregorianCalendar acceptTime;    @XmlElementRef(name = "agencyPrice", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> agencyPrice;    @XmlElementRef(name = "billId", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> billId;    @XmlElementRef(name = "billingPersonName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> billingPersonName;    @XmlElementRef(name = "branchName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> branchName;    @XmlElementRef(name = "chargeType", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> chargeType;    @XmlSchemaType(name = "dateTime")    protected XMLGregorianCalendar checkTime;    @XmlElementRef(name = "consigneeCardNO", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> consigneeCardNO;    @XmlElementRef(name = "consigneeInfomation", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<ConsigneeInfomation> consigneeInfomation;    @XmlElementRef(name = "consigneeName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> consigneeName;    @XmlElementRef(name = "consigneeSignName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> consigneeSignName;    @XmlElementRef(name = "consignorInfomation", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<ConsignorInfomation> consignorInfomation;    @XmlElementRef(name = "consignorName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> consignorName;    @XmlElementRef(name = "endStationName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> endStationName;    @XmlElementRef(name = "goodsInfomations", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<ArrayOfGoodsInfomation> goodsInfomations;    protected Boolean isInsurance;    protected Boolean isRouteRecord;    @XmlElementRef(name = "labelId", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> labelId;    @XmlSchemaType(name = "dateTime")    protected XMLGregorianCalendar limitTime;    @XmlElementRef(name = "packageBillId", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> packageBillId;    @XmlElementRef(name = "prepayPrice", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> prepayPrice;    @XmlElementRef(name = "procedureCharge", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> procedureCharge;    @XmlElementRef(name = "remark", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> remark;    @XmlElementRef(name = "route", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> route;    @XmlElementRef(name = "routePersonName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> routePersonName;    @XmlElementRef(name = "sendStationName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> sendStationName;    @XmlElementRef(name = "stationName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> stationName;    protected Float totalCharge;    protected Integer totalCount;    protected Float totalWeight;    @XmlElementRef(name = "transferName", namespace = "http://schemas.datacontract.org/2004/07/DataExchange.Utility.BussinessObject", type = JAXBElement.class, required = false)    protected JAXBElement<String> transferName;    @XmlSchemaType(name = "dateTime")    protected XMLGregorianCalendar travelTime;    //getter,setter

热点排行