java中xml进⾏报⽂发送和解析操作利⽤OKhttp⽹络框架,进⾏Maven项⽬管理
//报⽂发送
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.8.0</version>
</dependency>
//报⽂解析
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
</dependency>
报⽂⽰例
<STUDENT>
<AGE>18</AGE>
<NAME>
<XING>赵</XING>
<MING>明⽟</MING>
</NAME>
<NAME>
<XING>沙</XING>
<MING>明德</MING>
</NAME>
</STUDENT>
报⽂拼接
StringBuffer strBuff = new StringBuffer();
strBuff.append("<STUDENT>");
strBuff.append("<AGE>18</AGE>");
strBuff.append("<NAME>");
strBuff.append("<XING>赵</XING>");
strBuff.append("<MING>明⽟</MING>");
strBuff.append("</NAME>");
strBuff.append("<NAME>");
strBuff.append("<XING>沙</XING>");
strBuff.append("<MING>明德</MING>");
strBuff.append("</NAME>");
strBuff.append("</STUDENT>");
String xmlStr = String;
报⽂发送,post请求接⼝调⽤,xmlStr为xml格式请求参数体
public String postXml(String xmlStr){
//可改变请求参数体编码GBK/UTF-8
RequestBody body= ate(MediaType.parse("application/xml;charset=GBK"), xmlStr);
//url为接⼝地址
Request requestOk = new Request.Builder().url("192.168.0.103:8007").post(body).build();
//⽹络请求
OkHttpClient client = new OkHttpClient.Builder().retryOnConnectionFailure(true).build();
//可⼿动设置连接时长
OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS)
.readTimeout(60000, TimeUnit.MILLISECONDS)
.build();
//获取处理结果
Response response = null;
try {
response = wCall(requestOk).execute();
} catch (IOException e) {
e.printStackTrace();
}
//获取响应
String jsonString = response.body().string();
return jsonString;
}
报⽂解析,进⾏实体解析
//STUDENT为XML最⼤节点
JAXBContext context = wInstance(STUDENT.class);
Unmarshaller unmarshaller = ateUnmarshaller();
/
/jsonString为报⽂响应
STUDENT student = (STUDENT)unmarshaller.unmarshal(new StringReader(jsonString));实体类
//XmlRootElement注解是将类与XML元素进⾏映射,XML节点与属性⼤⼩写保持⼀致
@XmlRootElement(name ="STUDENT")
public class STUDENT implements Serializable {
private String AGE;
private List<NAME> NAME;
public String getAGE() {
return AGE;
}
public void setAGE(String AGE) {
this.AGE = AGE;
}
public List<NAME> getNAME() {
return NAME;
}
public void setNAME(List<NAME> NAME) {
this.NAME = NAME;
}
@Override
public String toString() {
return "Cccc{" +
"AGE='" + AGE + '\'' +
", NAME=" + NAME +
'}';
}
}
补充知识:Java发送内容为xml格式请求并解析返回json结果
封装成静态请求⽅法:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.ConnectException;
import java.MalformedURLException;
import java.URL;
import java.URLConnection;
/**
* 发送HTTP的⼀种⽅法
* GaoLiang
*/
public class HttpSendUtil {
public static String xmlPost(String urlStr, String xmlInfo) {
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
// con.setRequestProperty("Pragma:", "no-cache");
// con.setRequestProperty("Cache-Control", "no-cache");
// ⼀定要设置报⽂格式,否则发送失败
con.setRequestProperty("Content-Type", "text/xml");
OutputStreamWriter out = null;
try {
out = new OutputStream());
} catch (ConnectException e) {
/
/ e.printStackTrace();
return "Connection refused";
}
// System.out.println("urlStr=" + urlStr);
// System.out.println("xmlInfo=" + xmlInfo);
out.write(new Bytes("ISO-8859-1")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
// System.out.println(line);
stringBuilder.append(line);
}
String();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
在外部调⽤静态⽅法:
// 请求的xml服务地址
String url = url;
// 请求参数由于是⼿动拼写的参数很长看起来也不美观就不放了(⼿动哭脸)
String param = "" ;
// 发送请求调⽤上⾯的静态⽅法
String ret = xmlPost(url,param);
// 得到的是xml格式的返回结果但是⾥⾯的“<”,“>”都是“<”,“>”显⽰所以就需要解析,具体解析见下表ret = placeAll("<", "<");
ret = placeAll(">", ">");
ret = placeAll("&", "&");
ret = placeAll(""", "\"");
ret = placeAll("'", "\'");
// 因为接⼝最终要返回的是json格式所以还要把想要的值取出来解析成String
// 解析拿到节点⾥想要的值
Document document = DocumentHelper.parseText(ret);
// 根节点这⾥不⼀定要和根节点取⼀样的名字可⾃定义这么写是⽅便理解
Element root = RootElement();
// root节点下的⼦节点
Element business = root.element("business");
//解析完取到business节点下的值
String healthCode = StringValue();
/
/ 拼接返回值
Map<String,Object> thisResult = new HashMap();
thisResult.put("healthCode",healthCode);
resultList.add(thisResult);
// 统⼀返回值
Map<String,Object> returnset = new HashMap<>();java xml是什么
xml解析五种格式:
补充⼀下:xml返回结果类似于这样
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<business>我想要的值:healthCode</business >
</Root>
最后,由于时间⽐较匆忙,⼜没有接触过xml格式请求,确实折磨了我⼀阵⼦时间,搜了好多⼤神写的博客,最终整理出⾃⼰的。写的⽐较笨希望⼤家帮忙指点,互相学习。奥利给,冲了!
以上这篇java中xml进⾏报⽂发送和解析操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论