package com.chinaj.webframework.utils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.chinaj.yqb.mobilevo.CarAddVo;
import com.chinaj.yqb.vo.OpenPerPayVo;
public class XmlUtil {
/**
*
* @Title: Entity2XmlStr
* @author Xiao
* @Description: 根据实体生成XML字符串,注意对象属性与API接口大小写对应
* @param entity
* @throws Exception
* @return String
*/
public static String Entity2XmlStr(Object entity) throws Exception {
org.dom4j.Document doc = DocumentHelper.createDocument();
doc.setXMLEncoding("UTF-8");
Element ele = doc.addElement("req");
Field [] fs1 = entity.getClass().getDeclaredFields();
for (int i = 0; i < fs1.length; i++) {
Field f1 = fs1[i];
f1.setAccessible(true);
if(f1.get(entity) != null){
if(f1.get(entity).getClass() == Map.class ||
f1.get(entity).getClass() == HashMap.class){
//当存在子节点的时候。这边的子节点只能是Map的形式
Map<String, Object> childrenNode = (Map<String, Object>)f1.get(entity);
Element childrenEle = ele.addElement(f1.getName());
for (String key : childrenNode.keySet()) {
if(childrenNode.get(key) == null){
childrenEle.addElement(key, "");
}else{
childrenEle.addElement(key, childrenNode.get(key).toString());
}
}
ele.add(childrenEle);
}else{
ele.addElement(f1.getName()).addText(f1.get(entity) + "");
}
}else{
ele.addElement(f1.getName()).addText("");
}
}
return doc.asXML();
}
/**
*
* @Title: Map2XmlStr
* @author Xiao
* @Description: 根据一个MAP对象,转换成一串对应的XML字符串
* @param params
* @return
* @throws Exception
*/
public static String Map2XmlStr(Object obj) throws Exception {
if(obj.getClass() == Map.class){
return Map2XmlStr((Map)obj);
}else if(obj.getClass() == HashMap.class){
return Map2XmlStr((HashMap)obj);
}else{
throw new RuntimeException("传入的参数非MapOrHashMap格式!");
}
}
/**
*
* @Title: Map2XmlStr
* @author Xiao
* @Description: 根据一个MAP对象,转换成一串对应的XML字符串
* @param params
* @return
* @throws Exception
*/
public static String Map2XmlStr(Map<String, Object> params) throws Exception {
org.dom4j.Document doc = DocumentHelper.createDocument();
doc.setXMLEncoding("UTF-8");
Element ele = doc.addElement("req");
ele = getMapChildren(ele, params);
return doc.asXML();
}
public static Element getMapChildren(Element ele, Map<String, Object> params){
for (String key : params.keySet()) {
if(params.get(key) == null){
ele.addElement(key).addText("");
}else{
if(params.get(key).getClass() == Map.class || params.get(key).getClass() == HashMap.class){
Map<String, Object> chidlren = (Map<String, Object>) params.get(key);
if(chidlren.keySet().size() > 0){
Element chilNode = ele.addElement(key);
chilNode = getMapChildren(chilNode, chidlren);
}
}else{
ele.addElement(key).addText(params.get(key).toString());
}
}
}
return ele;
}
}
稍微记录下。有需要的。复制粘贴。修改一下引用就可以了。