使用场景:
接收XML格式的报文,转换成java实体对象
报文示例
<?xml version="1.0" encoding="utf-8"?>
<Packet>
<Head>
<RequestType>v7788</RequestType>
<ResponseCode>1</ResponseCode>
<ErrorCode>0000</ErrorCode>
<ErrorMessage>成功</ErrorMessage>
</Head>
<Body>
<CarModel>
<ModelCode>BFTBOEUA0008</ModelCode>
<ModelIDCode>BFTBOEUA000810A1</ModelIDCode>
<TradeName>北京福田戴姆勒汽车有限公司</TradeName>
<TradeCode>M03557</TradeCode>
<Brand>福田</Brand>
<BrandCode>FTB</BrandCode>
<Series>欧曼EST</Series>
<SeriesCode>FTBOE</SeriesCode>
<CarName>欧曼BJ5329CCY-AD</CarName>
<NoticeType>BJ5329CCY-AD</NoticeType>
<ConfigType>UA</ConfigType>
<CategoryName>载货汽车</CategoryName>
<CategoryCode>1</CategoryCode>
<DeptName>国产</DeptName>
<DeptCode>0</DeptCode>
<UpdateVersion>2.02009040376E11</UpdateVersion>
<BizVersion>201401</BizVersion>
<ValidStatus>00</ValidStatus>
<ObtainCreateTime>20200904180547916</ObtainCreateTime>
<ObtainUpdateTime>20200904180547916</ObtainUpdateTime>
</CarModel>
<CarModel>
<ModelCode>BJLMJAUA0002</ModelCode>
<ModelIDCode>BJLMJAUA000250A1</ModelIDCode>
<TradeName>奥龙汽车有限公司</TradeName>
<TradeCode>M03419</TradeCode>
<Brand>久龙</Brand>
<BrandCode>JLM</BrandCode>
<Series>久龙摆臂式垃圾车</Series>
<SeriesCode>JLMJA</SeriesCode>
<CarName>久龙ALA5040ZBSBJ6</CarName>
<NoticeType>ALA5040ZBSBJ6</NoticeType>
<ConfigType>UA</ConfigType>
<CategoryName>专用汽车</CategoryName>
<CategoryCode>5</CategoryCode>
<DeptName>国产</DeptName>
<DeptCode>0</DeptCode>
<UpdateVersion>2.02009040377E11</UpdateVersion>
<BizVersion>201401</BizVersion>
<ValidStatus>00</ValidStatus>
<ObtainCreateTime>20200904180652193</ObtainCreateTime>
<ObtainUpdateTime>20200904180652193</ObtainUpdateTime>
</CarModel>
</Body>
</Packet>
报文对应实体
@XmlRootElement
public class Head implements Serializable{
private static final long serialVersionUID = 228622503545455287L;
@XmlElement(name = "RequestType")
private String requestType;
@XmlElement(name = "ResponseCode")
private String responseCode;
@XmlElement(name = "ErrorCode")
private String errorCode;
@XmlElement(name = "ErrorMessage")
private String errorMessage;
@XmlTransient
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
@XmlTransient
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
@XmlTransient
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
@XmlTransient
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
@XmlRootElement
public class Body implements Serializable {
@XmlElement(name = "CarModel")
private List<PrpDCarModelInfo> carModelInfoList;
@XmlTransient
public List<PrpDCarModelInfo> getCarModelInfoList() {
return carModelInfoList;
}
public void setCarModelInfoList(List<PrpDCarModelInfo> carModelInfoList) {
this.carModelInfoList = carModelInfoList;
}
}
@XmlRootElement(name = "Packet")
public class PrpDCarModelInfos implements Serializable{
private static final long serialVersionUID = -904851922619726718L;
@XmlElement(name = "Head")
private Head head;
@XmlElement(name = "Body")
private Body body;
@XmlTransient
public Head getHead() {
return head;
}
public void setHead(Head head) {
this.head = head;
}
@XmlTransient
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
}
@XmlRootElement
public class PrpDCarModelInfo implements java.io.Serializable {
private static final long serialVersionUID = 419613135843500977L;
@XmlElement(name = "ModelCode")
private String modelCode; // 行业车型编码
@XmlElement(name = "ModelIDCode")
private String modelIdCode; // 车型识别编码
@XmlElement(name = "TradeName")
private String tradeName; // 厂商名称
@XmlElement(name = "TradeCode")
private String tradeCode; // 厂商代码
@XmlElement(name = "Brand")
private String brand; // 品牌名称
@XmlElement(name = "BrandCode")
private String brandCode; // 品牌代码
@XmlElement(name = "Series")
private String series; // 车系名称
@XmlElement(name = "SeriesCode")
private String seriesCode; // 车系代码
@XmlElement(name = "CarName")
private String carName; // 车款名称
@XmlElement(name = "NoticeType")
private String noticeType; // 公告型号
@XmlElement(name = "ConfigType")
private String configType; // 配置款型编码
@XmlElement(name = "CategoryName")
private String categoryName; // 类别名称
@XmlElement(name = "CategoryCode")
private String categoryCode; // 类别编码
@XmlElement(name = "DeptName")
private String deptName; // 系别名称
@XmlElement(name = "DeptCode")
private String deptCode; // 系别代码
@XmlElement(name = "UpdateVersion")
private BigDecimal updateVersion; // 车型更新版本号
@XmlElement(name = "BizVersion")
private BigDecimal bizVersion; // 车型业务版本号
@XmlElement(name = "ValidStatus")
private String validStatus; // 效力状态
@XmlElement(name = "ObtainCreateTime")
private String screateTime;
private Date obtaincreateTime; // 入库时间:精确到毫秒
@XmlElement(name = "ObtainUpdateTime")
private String modifyTime;
private Date obtainupdateTime; // 更新时间:精确到毫秒
@XmlElement(name = "ReplacementValue")
private BigDecimal replacementValue;
@XmlTransient
public String getModelCode() {
return modelCode;
}
public void setModelCode(String modelCode) {
this.modelCode = modelCode;
}
@XmlTransient
public String getModelIdCode() {
return modelIdCode;
}
public void setModelIdCode(String modelIdCode) {
this.modelIdCode = modelIdCode;
}
@XmlTransient
public String getTradeName() {
return tradeName;
}
public void setTradeName(String tradeName) {
this.tradeName = tradeName;
}
@XmlTransient
public String getTradeCode() {
return tradeCode;
}
public void setTradeCode(String tradeCode) {
this.tradeCode = tradeCode;
}
@XmlTransient
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@XmlTransient
public String getBrandCode() {
return brandCode;
}
public void setBrandCode(String brandCode) {
this.brandCode = brandCode;
}
@XmlTransient
public String getSeries() {
return series;
}
public void setSeries(String series) {
this.series = series;
}
@XmlTransient
public String getSeriesCode() {
return seriesCode;
}
public void setSeriesCode(String seriesCode) {
this.seriesCode = seriesCode;
}
@XmlTransient
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
@XmlTransient
public String getNoticeType() {
return noticeType;
}
public void setNoticeType(String noticeType) {
this.noticeType = noticeType;
}
@XmlTransient
public String getConfigType() {
return configType;
}
public void setConfigType(String configType) {
this.configType = configType;
}
@XmlTransient
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
@XmlTransient
public String getCategoryCode() {
return categoryCode;
}
public void setCategoryCode(String categoryCode) {
this.categoryCode = categoryCode;
}
@XmlTransient
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@XmlTransient
public String getDeptCode() {
return deptCode;
}
public void setDeptCode(String deptCode) {
this.deptCode = deptCode;
}
@XmlTransient
public BigDecimal getBizVersion() {
return bizVersion;
}
public void setBizVersion(BigDecimal bizVersion) {
this.bizVersion = bizVersion;
}
@XmlTransient
public BigDecimal getUpdateVersion() {
return updateVersion;
}
public void setUpdateVersion(BigDecimal updateVersion) {
this.updateVersion = updateVersion;
}
@XmlTransient
public String getValidStatus() {
return validStatus;
}
public void setValidStatus(String validStatus) {
this.validStatus = validStatus;
}
@XmlTransient
public Date getObtaincreateTime() {
if (screateTime != null) {
Date da = new Date();
try {
da = DateUtils.parseDate(screateTime, "yyyyMMddHHmmssSSS");
} catch (Exception e) {
}
return da;
}
return obtaincreateTime;
}
public void setObtaincreateTime(Date obtaincreateTime) {
this.obtaincreateTime = obtaincreateTime;
}
@XmlTransient
public Date getObtainupdateTime() {
if (modifyTime != null) {
Date da = new Date();
try {
da = DateUtils.parseDate(modifyTime, "yyyyMMddHHmmssSSS");
} catch (Exception e) {
}
return da;
}
return obtainupdateTime;
}
public void setObtainupdateTime(Date obtainupdateTime) {
this.obtainupdateTime = obtainupdateTime;
}
@XmlTransient
public String getScreateTime() {
return screateTime;
}
public void setScreateTime(String screateTime) {
this.screateTime = screateTime;
}
@XmlTransient
public String getModifyTime() {
return modifyTime;
}
public void setModifyTime(String modifyTime) {
this.modifyTime = modifyTime;
}
@XmlTransient
public BigDecimal getReplacementValue() {
return replacementValue;
}
public void setReplacementValue(BigDecimal replacementValue) {
this.replacementValue = replacementValue;
}
}
需要使用到的工具类方法
/**
* 将获取的xml文件数据流,转换成String类型
*
* @param bufferedReader 输入流文本缓冲字符
* @return StringBuffer 字符缓存
*/
private StringBuffer transferInputToString(BufferedReader bufferedReader) {
StringBuffer xml = new StringBuffer("");
if (bufferedReader == null)
return xml;
try {
String line = null;
while ((line = bufferedReader.readLine()) != null) {
xml.append(line);
}
} catch (IOException e) {
exlog("读取的xml文件进行转换出错!", e);
}
return xml;
}
/**
* xml文件转换Bean对象
*
* @param clazz
* @param xmlStr
* @return
*/
public static Object xml2Bean(Class clazz, String xmlStr) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
操作xml报文文件
FileInputStream fis = new FileInputStream("D:\\vehicle.xml");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));
StringBuffer strResponseXML = transferInputToString(bufferedReader);
String resultXml = strResponseXML.toString();
PrpDCarModelInfos carInfos = PrpDCarModelInfos carInfos = (PrpDCarModelInfos) XmlUtil.xml2Bean(PrpDCarModelInfos.class, resultXml);
Head head = carInfos.getHead();
String respCode = head.getResponseCode();
String errCode = head.getErrorCode();
Body body = carInfos.getBody();
List<PrpDCarModelInfo> list = body.getCarModelInfoList();
版权声明:本文为u012724331原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。