soap文档
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?disco
soap协议:简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息;
可以把它堪称一个xml ,但是有不规则的字段节点
1.首先把这个xml文件转化为json格式(注意引入的maven,org.json)package com.shands;
import com.alibaba.fastjson.JSON;
import com.shands.demo.Demo;
import org.json.JSONObject;
import org.json.XML;
import java.io.*;
public class Demo1 {
public static void main(String[] args) {
String path = "D:cann.xml.xml";
String s = readTxt(path);
JSonObject soapDatainJsonObject = XML.toJSonObject(s);
String s1 = soapDatainJsonObject.toString();
System.out.println(s1);
}
public static String readTxt(String path) {
StringBuilder content = new StringBuilder();
try {
String code = resolveCode(path);
File file = new File(path);
InputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is, code);
BufferedReader br = new BufferedReader(isr);
String str = "";
while (null != (str = br.readLine())) {
content.append(str);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("读取文件:" + path + "失败!");
}
return content.toString();
}
public static String resolveCode(String path) throws Exception {
InputStream inputStream = new FileInputStream(path);
byte[] head = new byte[3];
inputStream.read(head);
String code = "gb2312"; //或GBK
if (head[0] == -1 && head[1] == -2)
code = "UTF-16";
else if (head[0] == -2 && head[1] == -1)
code = "Unicode";
else if (head[0] == -17 && head[1] == -69 && head[2] == -65)
code = "UTF-8";
inputStream.close();
System.out.println(code);
return code;
}
}
2.优化json文件,(如果有不规则的节点名把不规则的节点名去掉如soap: hc: 之类的)
3.复制json 文件转为对象
4.生成完整的类对象
(如果有前缀,那么在jsonfiled 中加上 如
@JSonField(name = "xmlns:soap") private String _$XmlnsSoap53;
)
5.在controller 中用string 或者HttpServletRequest接受到xml 6.使用org.jsonJSONObject 转为对象org.json.JSonObject soapDatainJsonObject = XML.toJSonObject(xml); String s1 = soapDatainJsonObject.toString(); demo2 demo= JSON.parseObject(s1, demo2 .class);



