笔记:java转XML

时间:2023-03-08 19:51:34
 package com.deppon.oms.module.client.pushToTianFangkeji.domain;

 import java.util.ArrayList;
import java.util.List; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="Request")
public class EmpPushToTianFangRequest {
private MsgHead msgHead;
private Body Body; /**
* @return the msgHead
*/
public MsgHead getMsgHead() {
return msgHead;
} /**
* @param msgHead the msgHead to set
*/
public void setMsgHead(MsgHead msgHead) {
this.msgHead = msgHead;
} /**
* @return the body
*/
public Body getBody() {
return Body;
} /**
* @param body the body to set
*/
public void setBody(Body body) {
Body = body;
} /**
* 内部类MSgHead
* @author 342290
*
*/
public static class MsgHead{
private String tradeNo;
private String processCode;
private String ecCompanyCode;
private String expressCompanyCode;
/**
* @return the tradeNo
*/
public String getTradeNo() {
return tradeNo;
}
/**
* @param tradeNo the tradeNo to set
*/
public void setTradeNo(String tradeNo) {
this.tradeNo = tradeNo;
}
/**
* @return the processCode
*/
public String getProcessCode() {
return processCode;
}
/**
* @param processCode the processCode to set
*/
public void setProcessCode(String processCode) {
this.processCode = processCode;
}
/**
* @return the ecCompanyCode
*/
public String getEcCompanyCode() {
return ecCompanyCode;
}
/**
* @param ecCompanyCode the ecCompanyCode to set
*/
public void setEcCompanyCode(String ecCompanyCode) {
this.ecCompanyCode = ecCompanyCode;
}
/**
* @return the expressCompanyCode
*/
public String getExpressCompanyCode() {
return expressCompanyCode;
}
/**
* @param expressCompanyCode the expressCompanyCode to set
*/
public void setExpressCompanyCode(String expressCompanyCode) {
this.expressCompanyCode = expressCompanyCode;
}
} /**
* 内部类Body
* 342290 陈军
*/
public static class Body{ List<DeliveryPerson> deliveryPersons = new ArrayList<DeliveryPerson>(); /**
* @return the deliveryPersons
*/
@XmlElementWrapper(name="deliveryPersons")
@XmlElements(value = {@XmlElement(name = "deliveryPerson", type = DeliveryPerson.class) })
public List<DeliveryPerson> getDeliveryPersons() {
return deliveryPersons;
} /**
* @param deliveryPersons the deliveryPersons to set
*/
public void setDeliveryPersons(List<DeliveryPerson> deliveryPersons) {
this.deliveryPersons = deliveryPersons;
} } /**
* 内部类DeliveryPerson
* 342290 陈军
*/
public static class DeliveryPerson{
//快递人员编号
private String deliveryPersonID;
//快递人员姓名
private String name;
//联系电话
private String phone;
//证件类型:07000001二代身份证;07000002护照
private String certificatesType;
//证件号码
private String certificateNo;
//人员常驻地址
private String Address;
//入职时间,格式为yyyy-mm-dd hh:mm:ss
private String entryTime;
//离职时间,格式为yyyy-mm-dd hh:mm:ss
private String quitTime;
//快递人员从业资格证号码
private String deliveryNo;
//从业资格证等级:08000001初级,08000002中级,08000003高级
private String deliveryLevel;
//人员在职状态:09000001在职,09000002非在职
private String Status;
//备注
private String Remark; /**
* @return the deliveryPersonID
*/
public String getDeliveryPersonID() {
return deliveryPersonID;
}
/**
* @param deliveryPersonID the deliveryPersonID to set
*/
public void setDeliveryPersonID(String deliveryPersonID) {
this.deliveryPersonID = deliveryPersonID;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the phone
*/
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the certificatesType
*/
public String getCertificatesType() {
return certificatesType;
}
/**
* @param certificatesType the certificatesType to set
*/
public void setCertificatesType(String certificatesType) {
this.certificatesType = certificatesType;
}
/**
* @return the certificateNo
*/
public String getCertificateNo() {
return certificateNo;
}
/**
* @param certificateNo the certificateNo to set
*/
public void setCertificateNo(String certificateNo) {
this.certificateNo = certificateNo;
}
/**
* @return the address
*/
public String getAddress() {
return Address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
Address = address;
}
/**
* @return the entryTime
*/
public String getEntryTime() {
return entryTime;
}
/**
* @param entryTime the entryTime to set
*/
public void setEntryTime(String entryTime) {
this.entryTime = entryTime;
}
/**
* @return the quitTime
*/
public String getQuitTime() {
return quitTime;
}
/**
* @param quitTime the quitTime to set
*/
public void setQuitTime(String quitTime) {
this.quitTime = quitTime;
}
/**
* @return the deliveryNo
*/
public String getDeliveryNo() {
return deliveryNo;
}
/**
* @param deliveryNo the deliveryNo to set
*/
public void setDeliveryNo(String deliveryNo) {
this.deliveryNo = deliveryNo;
}
/**
* @return the deliveryLevel
*/
public String getDeliveryLevel() {
return deliveryLevel;
}
/**
* @param deliveryLevel the deliveryLevel to set
*/
public void setDeliveryLevel(String deliveryLevel) {
this.deliveryLevel = deliveryLevel;
}
/**
* @return the status
*/
public String getStatus() {
return Status;
}
/**
* @param status the status to set
*/
public void setStatus(String status) {
Status = status;
}
/**
* @return the remark
*/
public String getRemark() {
return Remark;
}
/**
* @param remark the remark to set
*/
public void setRemark(String remark) {
Remark = remark;
}
} }
 public static void main(String []args){
EmpPushToTianFangRequest request = new EmpPushToTianFangRequest();
EmpPushToTianFangRequest.MsgHead msgHead = new EmpPushToTianFangRequest.MsgHead();
EmpPushToTianFangRequest.Body body = new EmpPushToTianFangRequest.Body(); msgHead.setEcCompanyCode("95353");
msgHead.setExpressCompanyCode("Deppon");
msgHead.setProcessCode("10666666666666666"); EmpPushToTianFangRequest.DeliveryPerson deliveryPerson1 = new EmpPushToTianFangRequest.DeliveryPerson();
deliveryPerson1.setAddress("明珠路88号");
deliveryPerson1.setDeliveryNo("360781199207100033");
deliveryPerson1.setEntryTime("2011-11-11 11:11:11"); EmpPushToTianFangRequest.DeliveryPerson deliveryPerson2 = new EmpPushToTianFangRequest.DeliveryPerson();
deliveryPerson2.setAddress("明珠路99号");
deliveryPerson2.setDeliveryNo("360666666666666666");
deliveryPerson2.setEntryTime("2011-12-12 12:12:12"); List<EmpPushToTianFangRequest.DeliveryPerson> deliveryPersons = new ArrayList<EmpPushToTianFangRequest.DeliveryPerson>();
deliveryPersons.add(deliveryPerson1);
deliveryPersons.add(deliveryPerson2); body.setDeliveryPersons(deliveryPersons);
request.setMsgHead(msgHead);
request.setBody(body); try {
JAXBContext context = JAXBContext.newInstance(EmpPushToTianFangRequest.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(request, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
@XmlElementWrapper(name="deliveryPersons") 注解是关键