通过输入流获取的xml格式字符串转为json和map格式
xml格式字符串转json格式代码:
try {
ServletInputStream inputStream = getActionRequest().getInputStream();
String xml = IOUtils.toString(inputStream);
JSONObject xmlJSONObj = XML.toJSONObject(xml);
} catch (Exception e) {
e.printStackTrace();
}
转换前代码:
xml<?xml version="1.0" encoding="UTF-8"?>
<response version="1.0" systemType="NVMS-9000">
<status>success</status>
<content type="list">
<item id="08095753-fbb2-4bc1-81c3-54d67ac"/>
</content>
</response>
转换后代码:
xmlJSONObj{"response":{"version":"1.0","systemType":"NVMS-9000","status":"success","content":{"type":"list","item":{"id":"08095753-fbb2-4bc1-81c3-54d67ac"}}}}
xml格式字符串转为map格式代码:
try {
S
ServletInputStream inputStream = getActionRequest().getInputStream();
String xml = IOUtils.toString(inputStream);
JSONObject xmlJSONObj = XML.toJSONObject(xml);
Gson gson = new Gson();
HashMap map = gson.fromJson(xmlJSONObj.toString(), HashMap.class);
} catch (Exception e) {
e.printStackTrace();
}
转换前代码:
xml<?xml version="1.0" encoding="UTF-8"?>
<response version="1.0" systemType="NVMS-9000">
<status>success</status>
<content type="list">
<item id="08095753-fbb2-4bc1-81c3-54d67ac"/>
</content>
</response>
转换后代码:
{response={version=1.0, systemType=NVMS-9000, status=success, content={type=list, item={id=08095753-fbb2-4bc1-81c3-54d67ac}}}}