前言:
我們在項目開發中,有時候調用別人提供的接口,接收到的數據是xml,這時候爲了方便後續開發中的使用,需要解析xml並把數據轉化成json,今天這篇文章就講解下如何把xml轉化成json,並在這個過程中容易出現的問題。
正文:
一、測試項目的搭建
1.需要引入的pom依賴
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.0.9</version>
</dependency>
2.xml映射的對象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Programmer{
private String name;
private Integer age;
private String sex;
private String img;
}
3.轉化的工具類
public class Xml2Json {
public static <T> T convertToObject(String xml, Class<T> cla){
JSONObject object = XML.toJSONObject(xml);
String jsonData = object.toString();
System.err.println(jsonData);
//截取字符串
String jsonObject = jsonData.substring(jsonData.indexOf(":")+1, jsonData.length()-1);
//將json格式的字符串轉換成CertApplyRequest格式
Gson gson = new Gson();
T request = gson.fromJson(jsonObject, cla);
return request;
}
二、復現問題
1.測試代碼
public static void main(String[] args){
String s="<student name=\"zhangsan\">\n" +
" <sex>man</sex>\n" +
" <age>18</age>\n" +
"\t<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1" +
"&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>\n" +
"</student>\n";
System.out.println(s);
Programmer programmer = Xml2Json.convertToObject(s, Programmer.class);
System.out.println(programmer.toString());
}
2.運行結果:報錯啦!
<student name="zhangsan">
<sex>man</sex>
<age>18</age>
<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>
</student>
Exception in thread "main" cn.hutool.json.JSONException: Missing ';' in XML entity: &z at 121 [character 59 line 4]
at cn.hutool.json.JSONTokener.syntaxError(JSONTokener.java:373)
at cn.hutool.json.XMLTokener.nextEntity(XMLTokener.java:107)
at cn.hutool.json.XMLTokener.nextContent(XMLTokener.java:83)
at cn.hutool.json.XML.parse(XML.java:171)
at cn.hutool.json.XML.parse(XML.java:185)
at cn.hutool.json.XML.toJSONObject(XML.java:232)
at cn.hutool.json.XML.toJSONObject(XML.java:214)
at com.model.Xml2Json.convertToObject(Xml2Json.java:15)
at com.model.Xml2Json.main(Xml2Json.java:46)
Process finished with exit code 1
3.分析結果
一般出現上面的異常,基本都是xml裏的格式或者有特殊字符才報錯的,所以我們可以看下我們的xml裏的參數是否有特殊字符。可以看到img標籤裏的圖片地址有特殊字符&,所以這時候我們只要把&進行轉義,就可以啦。
三、解決問題
1.修改後的測試代碼:
public static void main(String[] args){
String s="<student name=\"zhangsan\">\n" +
" <sex>man</sex>\n" +
" <age>18</age>\n" +
"\t<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1" +
"&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>\n" +
"</student>\n";
String s1 = s.replaceAll("&", "&");
System.out.println(s);
System.out.println(s1);
Programmer programmer = Xml2Json.convertToObject(s1, Programmer.class);
System.out.println(programmer.toString());
}
2.運行結果:xml轉化json成功。
<student name="zhangsan">
<sex>man</sex>
<age>18</age>
<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>
</student>
<student name="zhangsan">
<sex>man</sex>
<age>18</age>
<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>
</student>
{"student":{"img":"https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111","sex":"man","name":"zhangsan","age":18}}
Xml2Json.Programmer(name=zhangsan, age=18, sex=man, img=https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111)
總結:
xml轉化josn報錯的時候基本上都是xml的格式不正確或者是數據裏有特殊字符,對了xml裏的標籤頭不用轉義,轉義後反而會報錯,原因在源碼裏。
我是阿達,一名喜歡分享知識的程序員,時不時的也會荒腔走板的聊一聊電影、電視劇、音樂、漫畫,這裏已經有382位小夥伴在等你們啦,感興趣的就趕緊來點擊關注我把,哪裏有不明白或有不同觀點的地方歡迎留言。