欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

XML数据拼接和解析代码总结

程序员文章站 2022-03-20 11:17:50
...

想要实现下面xml格式

<?xml version="1.0" encoding="utf-8"?>
<datapacket 
sign="NDQwMTExMThlMTE1MzEyM2Q3ZDE4MGNlZWI4MjBkNTc3ZmYxMTk4NzY" 
supplierCode="44011118">
<rowdata>
<masterdata>
<item
purplanNo="PP00000001"
 deptCode="44011121"
deptName="医大供应商"
 purchaserSupplierCode="YY000001"
 purchaserSupplierName="测试医院" 
purplanTime="2019-09-09 02:12:59" 
 expectTime="2019-09-19 02:13:05"
 createTime="2019-09-09 02:13:24"
 purchaseStatus="1" 
 masterRemark=""
 />
<detaildata>
<item
 detailId="1127"
erpDetailId="0027"
sgoodsSpecsCode="C00001"
sgoodsName="测试商品0"
sgoodsSpecs="10ml"
sgoodsUnit="支"
sgoodsQty="100"
factoryName="云南剂厂"
detailRemark="云南" />
<item
 detailId="1128"
erpDetailId="0028"
sgoodsSpecsCode="C00002"
sgoodsName="测试商品1"
sgoodsSpecs="20ml"
sgoodsUnit="支"
sgoodsQty="100"
factoryName="云南剂厂"
detailRemark="云南"/>
</detaildata>
</masterdata>
</rowdata>
</datapacket>

需要做一下操作,需要三个model,前两个分别查处索要数据,第三个进行数据操作


import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

/**
 * model
 * 
 * @author 
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "item")
public class McpYdPurchaseOrderDTO implements Serializable {

	private static final long serialVersionUID = 1L;

	
	@XmlAttribute
	public Long masterId;

	
	@XmlAttribute
	public String purplanNo;

	@XmlAttribute
	public String deptCode;

	
	@XmlAttribute
	public String deptName;

	
	@XmlAttribute
	public String purchaserSupplierCode;

	@XmlAttribute
	public String purchaserSupplierName;


	@XmlAttribute
	public String purplanTime;

	
	@XmlAttribute
	public String expectTime;

	@XmlAttribute
	public String createTime;


	@XmlAttribute
	public Integer purchaseStatus = 1;

	@XmlAttribute
	public String masterRemark;

	@XmlAttribute
	public String customerEnableCode;

}

 


import lombok.Data;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import java.io.Serializable;
import java.math.BigDecimal;
/**
 * 明细model
 * 
 * @author guoxinsong
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class McpYdPurchaseOrderItemDTO implements Serializable {

	private static final long serialVersionUID = 1L;

	
	@XmlAttribute
	public String customerEnableCode;


	@XmlAttribute
	public Long detailId;


	@XmlAttribute
	public Long erpDetailId;


	@XmlAttribute
	public String sgoodsSpecsCode;


	@XmlAttribute
	public String sgoodsName;


	@XmlAttribute
	public String sgoodsSpecs;


	@XmlAttribute
	public String sgoodsUnit;

	@XmlAttribute
	public BigDecimal sgoodsQty;

	@XmlAttribute
	public String factoryName;


	@XmlAttribute
	public String detailRemark;

}

 

import lombok.Data;

import javax.xml.bind.annotation.*;
import java.util.List;

@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "datapacket")
public class McpYdPurchaseOrderXmlDTO {
	@XmlAttribute
	private String supplierCode;

	@XmlAttribute
	private String sign;

	private Rowdata rowdata;

	@XmlAccessorType(XmlAccessType.FIELD)
	public static class Rowdata {

		private Masterdata masterdata;

		public Rowdata(Masterdata masterdata) {
			this.masterdata = masterdata;
		}

		@XmlAccessorType(XmlAccessType.FIELD)
		public static class Masterdata {
			private McpYdPurchaseOrderDTO item;
			private Detaildata detaildata;

			@XmlAccessorType(XmlAccessType.FIELD)
			public static class Detaildata {
				private List<McpYdPurchaseOrderItemDTO> item;

				public Detaildata(List<McpYdPurchaseOrderItemDTO> items) {
					this.item = items;
				}
			}

			public Masterdata(McpYdPurchaseOrderDTO item, Detaildata detaildata) {
				this.item = item;
				this.detaildata = detaildata;
			}
		}
	}
}

 

McpYdPurchaseOrderXmlDTO mcpYdPurchaseOrderXmlDTO = new McpYdPurchaseOrderXmlDTO();
		mcpYdPurchaseOrderXmlDTO.setRowdata(new McpYdPurchaseOrderXmlDTO.Rowdata(new McpYdPurchaseOrderXmlDTO.Rowdata.Masterdata(mcpYdPurchaseOrderDTO, new McpYdPurchaseOrderXmlDTO.Rowdata.Masterdata.Detaildata(mcpYdPurchaseOrderItemDTOList))));
		String requestXml = JaxbUtil.convertToXml(mcpYdPurchaseOrderXmlDTO);

就可以完成上面xml拼接了

解析返回值

<?xml version="1.0" encoding="utf-8"?>
<datapacket msg="1个供货单成功,2个供货单上传失败"
code="500" >
</datapacket>
JSONObject jsonObject = XML.toJSONObject(response);
						com.alibaba.fastjson.JSONObject result = JSON.parseObject(jsonObject.get("datapacket").toString());
						boolean error = result.get("code")!=null && result.get("code").equals(0);