XML字符串转java对象POJO
ity;
import java.util.List;
public class InventoryPojo {
public CEB621Message CEB621Message;
public InventoryPojo() {
}
public CEB621Message getCEB621Message() {
return CEB621Message;
}
public void setCEB621Message(CEB621Message cEB621Message) {
CEB621Message = cEB621Message;
}
public static class CEB621Message {
public BaseTransfer BaseTransfer;
public Inventory Inventory;
public CEB621Message() {
}
public BaseTransfer getBaseTransfer() {
return BaseTransfer;
}
public void setBaseTransfer(BaseTransfer baseTransfer) {
BaseTransfer = baseTransfer;
}
public Inventory getInventory() {
return Inventory;
}
public void setInventory(Inventory inventory) {
Inventory = inventory;
}
}
public static class BaseTransfer {
public String copCode;
public String dxpMode;
public String copName;
public String dxpId;
public BaseTransfer() {
}
public String getCopCode() {
return copCode;
}
public void setCopCode(String copCode) {
}
public String getDxpMode() {
return dxpMode;
}
public void setDxpMode(String dxpMode) {
this.dxpMode = dxpMode;
}
public String getCopName() {
return copName;
}
public void setCopName(String copName) {
}
public String getDxpId() {
return dxpId;
}
public void setDxpId(String dxpId) {
this.dxpId = dxpId;
}
}
public static class Inventory {
public InventoryHead InventoryHead;
public List<InventoryList> InventoryList;
public Inventory() {
}
public InventoryHead getInventoryHead() {
return InventoryHead;
}
public void setInventoryHead(InventoryHead inventoryHead) {
InventoryHead = inventoryHead;
}
public List<InventoryList> getInventoryList() {
return InventoryList;
}
public void setInventoryList(List<InventoryList> inventoryList) {
InventoryList = inventoryList;
}
}
}
POJO必须跟json格式对上可以去json 格式化⼀下---------------------------------------------------⼯具类--------------------------------------------------------
package cst.spmBase.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
ity.InventoryHead;
ity.InventoryList;
ity.InventoryPojo;
public class Xml2JsonUtil {
/**
*
* 转换⼀个xml格式的字符串到json格式
*
* @param xml
* xml格式的字符串
* @return 成功返回json 格式的字符串;失败反回null
*/
public static JSONObject xml2JSON(String xml) { JSONObject obj = new JSONObject();
try {
InputStream is = new Bytes("utf-8")); SAXBuilder sb = new SAXBuilder(false);
Document doc = sb.build(is);
Element root = RootElement();
obj.Name(), iterateElement(root));
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
fastjson字符串转数组* 转换⼀个xml格式的字符串到json格式
*
* @param file
* java.io.File实例是⼀个有效的xml⽂件
* @return 成功反回json 格式的字符串;失败反回null
*/
public static Object xml2JSON(File file, Class<?> clazz) { JSONObject obj = new JSONObject();
try {
SAXBuilder sb = new SAXBuilder(false);
Document doc = sb.build(file);
Element root = RootElement();
obj.Name(), iterateElement(root));
String object = String();
object = place("[", "");
object = place("]", "");
object = place("\"InventoryList\":{", "\"InventoryList\":[{"); object = place("}}}}", "}]}}}");
System.out.println(object);
Object jsonObj = JSON.parseObject(object, clazz);
System.out.println(jsonObj);
return jsonObj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* ⼀个迭代⽅法
*
* @param element
* : org.jdom.Element
* @return java.util.Map 实例
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map iterateElement(Element element) {
List<Element> jiedian = Children();
Element et = null;
Map obj = new HashMap();
List list = null;
for (int i = 0; i < jiedian.size(); i++) {
list = new LinkedList();
et = (Element) (i);
if (et.getTextTrim().equals("")) {
if (et.getChildren().size() == 0)
continue;
if (Name())) {
list = (List) (et.getName());
}
list.add(iterateElement(et));
obj.Name(), list);
} else {
if (Name())) {
list = (List) (et.getName());
}
list.TextTrim());
obj.Name(), list);
}
}
return obj;
}
// 测试
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
File file =new File("C:\\Users\\Administrator\\Desktop\\ZJ\\l");
InventoryPojo inventoryPojo = new InventoryPojo();
inventoryPojo = (InventoryPojo)xml2JSON(file, InventoryPojo.class);
InventoryHead head= CEB621Message().getInventory().getInventoryHead();
System.out.String());
List<InventoryList> list = CEB621Message().getInventory().getInventoryList();
for(InventoryList inventoryList : list) {
System.out.String());
}
}
}
这⾥提供了两种⽅法⼀种是file⽂件解析成json ⼀种是XML字符串解析成json  2。第⼀步吧xml解析成json  第⼆步因为⼯具类使⽤list.add往⾥⾯放数据所以解析完成以后每个字段都会带[]中括号,所以⽤replace 吧[]去掉,但是有可能会把真的⽤到[]去掉,所以再把⽤到得[]替换上去。最后转为实体。。。。。⽅法很笨很笨但是能⽤后⾯会修改⼯具类

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。