map转xml java_xml与map互转

/**

* 将Map转为XML,默认root标签为PACKET;

* 由于List中的元素没有名称,默认将List的名称_LIST>部分替换为_INFO>,作为List中元素的名字;

* 暂不考虑List中的元素也是List的情况。

* @param version 例如1.0

* @param encoding 例如UTF-8或者GBK

* @param packetType 例如REQUEST或者RESPONSE

* @param requestType 例如N01

* @param param BODY体中的内容

* @return

*/

public static String mapToXml(String version, String encoding,

String packetType, String requestType, Map param) {

StringBuffer sb = new StringBuffer("<?xml version=\"");

sb.append(version).append("\" encoding=\"").append(encoding)

.append("\"?>

.append(version).append("\">").append("

").append("ebaobj_test")

.append("").append("111111").append("")

.append(requestType).append("").append("

");

if (param != null && !param.isEmpty()) {

parseMapToXml(sb, param);

}

sb.append("");

return sb.toString();

}

/**

* 将XML转为Map,只取BODY里面的东西;

* 暂不考虑List中的元素还是List的情况。

*

* @param xml

* @return

* @throws JDOMException

* @throws IOException

*/

public static Map xmlToMap(String xml)

throws JDOMException, IOException {

if (xml == null || xml.length() == 0) {

return new HashMap();

}

StringReader reader = new StringReader(xml);

InputSource source = new InputSource(reader);

SAXBuilder sax = new SAXBuilder();

Document doc = sax.build(source);

Element root = doc.getRootElement();

List nodes = root.getChildren();

return (Map)parseXmlToMap(nodes).get("BODY");

}

// 将XML字符串转为Map

private static Map parseXmlToMap(List subNodes) {

Map subMap = new HashMap(subNodes.size());

List subSubNodes;

Element subEl = null;

String subElementName;

if (subNodes == null || subNodes.isEmpty()) {

return subMap;

}

for (Object subNode : subNodes) {

subEl = (Element) subNode;

subElementName = subEl.getName();

subSubNodes = subEl.getChildren();

if (subElementName.endsWith("_LIST")) {

// 当前元素是一个List

subMap.put(subElementName, parseXmlToList(subSubNodes));

} else if (!subSubNodes.isEmpty()) {

// 当前元素是一个Map

subMap.put(subElementName, parseXmlToMap(subSubNodes));

} else {

// 当前元素是一个简单元素

subMap.put(subElementName, subEl.getText());

}

}

return subMap;

}

// 将XML字符串转为Map,暂不考虑List中的元素还是List的情况

private static List> parseXmlToList(List subNodes) {

List> subList = new ArrayList>(

subNodes.size());

if (subNodes == null || subNodes.isEmpty()) {

return subList;

}

List subSubNodes;

Element subEl = null;

for (Object subNode : subNodes) {

subEl = (Element) subNode;

subSubNodes = subEl.getChildren();

if (subSubNodes.isEmpty()) {

// 空元素项

subList.add(new HashMap());

} else {

subList.add(parseXmlToMap(subSubNodes));

}

}

return subList;

}

// 将Map对象转为XML

private static void parseMapToXml(StringBuffer sb, Map param) {

if (param == null || param.isEmpty()) {

return;

}

String key;

Object value;

for (Entry entry : param.entrySet()) {

key = entry.getKey();

value = entry.getValue();

if (value instanceof List) {

sb.append("

sb.append(key);

sb.append(">");

parseListToXml(sb,

generateElementName(key), (List>) value);

sb.append("");

sb.append(key);

sb.append(">");

} else if (value instanceof Map) {

sb.append("

sb.append(key);

sb.append(">");

parseMapToXml(sb, (Map) value);

sb.append("");

sb.append(key);

sb.append(">");

} else {

sb.append("

sb.append(key);

sb.append(">");

sb.append(value);

sb.append("");

sb.append(key);

sb.append(">");

}

}

}

/**

* 将List对象转为XML,由于List中的元素没有名称,

* 默认将List的名称_LIST>部分替换为_INFO>,作为List中元素的名字;

* 暂不考虑List中的元素也是List的情况。

*/

private static void parseListToXml(StringBuffer sb, String elementName,

List> param) {

if (param == null || param.isEmpty()) {

return;

}

for (Map element : param) {

sb.append("

sb.append(elementName);

sb.append(">");

parseMapToXml(sb, element);

sb.append("");

sb.append(elementName);

sb.append(">");

}

}

// 将List的名称_LIST部分替换为_INFO,作为List中元素的名字

private static String generateElementName(String listName) {

if (listName == null || !listName.contains("_LIST")) {

return listName + "_INFO";

}

return listName.substring(0, listName.lastIndexOf("_LIST")) + "_INFO";

}

xml格式:

数据采用XML格式的文件,字符编码为GBK。

map格式:

/**

* 方案一:将file转换为字符串base64  需要单独传入文件名,文件路径

*      文件,文件名,文件路径可以放在同一个map下

* @throws DocumentException

* @throws SAXException

* @throws IOException

* @throws ParserConfigurationException

* @throws JDOMException

*/

public void test() throws DocumentException, SAXException, IOException, ParserConfigurationException, JDOMException{

System.out.println("====TestCheck人工核保测试   start====");

File underwritingPhoto1=new File("D:/1.png");

File underwritingPhoto2=new File("D:/2.png");

String base64File1=FileStreamUtil.fileToBase64(underwritingPhoto1);

String base64File2=FileStreamUtil.fileToBase64(underwritingPhoto2);

Map underwritingInfo1=new HashMap();

Map underwritingInfo2=new HashMap();

List> underwritingInfoList=new ArrayList>();

Map manualUnderwriting=new HashMap();

Map body=new HashMap();

underwritingInfo1.put("UNDERWRITING_PHOTO",base64File1 );//文件流

underwritingInfo2.put("UNDERWRITING_PHOTO",base64File2);

underwritingInfoList.add(underwritingInfo1);

underwritingInfoList.add(underwritingInfo2);

manualUnderwriting.put("UNDERWRITING_INFO_LIST", underwritingInfoList);

manualUnderwriting.put("IF_CANCEL_INSURE", "N");//是否取消投保

manualUnderwriting.put("INSURE_SEQUENCE_NO", "001");//投保流水号

body.put("MANUAL_UNDERWRITING", manualUnderwriting);

String xmlstr= CiitcXmlUtil.mapToXml("1.0", "GBK", "REQUEST", "N01", body);

Map map=new HashMap();

map.put("xmlstr", xmlstr);

System.out.println("====TestCheck人工核保测试   xmlstr===="+xmlstr);

plateCheckAction.peopleCheckInfBase64(map);

// HttpProxy.send("http://localhost:8080/egis-scms-insu/scms/insu/peopleCheckInfBase64.do", map, Map.class);

System.out.println("====TestCheck人工核保测试   end====");

}

/**

* 方案二:将file直接保存在Map里。文件名,文件路径,文件一起被传过去

*      这种方式行不通。

*      如果将file传入Map那么取出来的时候,Object是无法转换为File的。

*      会提示String不能转换为F   *

*      如果存入的时候,Map那么意味着,这个map只能存文件,不能存入其他非文件类型数据

*      如果单独传文件可以这样,但是我们既然要传map肯定是希望map中放入更多的值,要不然就不会用map直接传一个file过去了

*      但是如果想要再存入一些其他数据,就要另外再定义Map   *

*      多个map就必须放在一个list中,List>呢还是定义  List>呢?

*      如果定义为List>, list.add(fileMap);时候会报错。因为list里面规定的是object的map而如果存入file的map就就会编译错误

*      如果定义为 List>那就更不可能实现了

*

*      如果map定义为Map map ==map.put("file",file);//File file=new File("d://input.jpg");如果偷偷的存入一个file进去,这时候也是当作一个字符串文件路径存进去的。而不是File类型的文件。

*      因为在取值的时候,没有办法用File类型去接收。接收的时候,只能是String或者object类型。不能是Flie类型

*      如果用Object类型接口。强转为File类型,会提示报错:java.lang不能转为File   *

*      我做了一个实验:如果直接传输一个map过去,map中put了file则可以强转为File类型

*      如果将map转为xml,过去以后再将xml转为map。这时候。map里面拿到的所有的Object全部视string类型

*      虽然xlmtoMap方法中返回来的是Map 这样一转以后,在强转为File的时候,会报string不能转为File的错误   *

* 方案三:原因同方案二,无法强转为bype[],因为xml转map以后,map中的所有值补视为String类型,无法强转为byte[]

*

*      总结:目前只能采取方案一传入BASE64文件内容

*          方案二,方案三传入File,byte[]均因为xml与map的转换过程,map的value值无法强制转换为原始的File与byte[]类型

*

* @throws DocumentException

* @throws SAXException

* @throws IOException

* @throws ParserConfigurationException

* @throws JDOMException

*/

public void test2() throws DocumentException, SAXException, IOException, ParserConfigurationException, JDOMException{

System.out.println("====test2人工核保测试   start====");

File file1=new File("D:/1.png");

File file2=new File("D:/2.png");

Map underwritingInfo1=new HashMap();

Map underwritingInfo2=new HashMap();

List> underwritingInfoList=new ArrayList>();

Map manualUnderwriting=new HashMap();

Map body=new HashMap();

underwritingInfo1.put("UNDERWRITING_PHOTO",file1 );//文件流

underwritingInfo2.put("UNDERWRITING_PHOTO",file2);

underwritingInfoList.add(underwritingInfo1);

underwritingInfoList.add(underwritingInfo2);

//   for (Map file : underwritingInfoList) {

//    Object o=file.get("UNDERWRITING_PHOTO");

//    File f=(File)o;

//    System.out.println(f.getAbsolutePath());

//   }

manualUnderwriting.put("UNDERWRITING_INFO_LIST", underwritingInfoList);

manualUnderwriting.put("IF_CANCEL_INSURE", "N");//是否取消投保

manualUnderwriting.put("INSURE_SEQUENCE_NO", "001");//投保流水号

body.put("MANUAL_UNDERWRITING", manualUnderwriting);

String xmlstr= CiitcXmlUtil.mapToXml("1.0", "GBK", "REQUEST", "N01", body);

Map map=new HashMap();

map.put("xmlstr", xmlstr);

System.out.println("====TestCheck人工核保测试   xmlstr===="+xmlstr);

plateCheckAction.peopleCheckInfFiles(map);

// HttpProxy.send("http://localhost:8080/egis-scms-insu/scms/insu/peopleCheckInfTytes.do", map, Map.class);

System.out.println("====test2人工核保测试   end====");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值