相同的代碼,讀取未格式化xml和已格式化xml,未格式化的解析起來報錯,代碼很簡單
1.java解析代碼
// getResponseContent(fileName) 從指定文件名中讀取文件內容作爲字符串
String responseXML = getResponseContent("content");
// xml字符串 轉換爲 json字符串
String responseJsonStr = new net.sf.json.xml.XMLSerializer().read(responseXML).toString();
// json字符串 轉換 json對象
JSONObject responseJson = net.sf.json.JSONObject.fromObject(responseJsonStr);
// 獲取指定節點
JSONArray details = responseJson.getJSONArray("details");
2.解析未格式化xml
未格式化的xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><searchdetail totalRecords="1" pageSize="1" requestId="201910311452000033"><details><transaction><player playerId="abc" partnerId="bcd"/><detail transactionId="43308923710" transactionDate="20191031 03:53:00" currency="CNY" game="FuStarH5" transactionSubType="Wager" handId="13809462143" amount="-2.50"/></transaction></details></searchdetail>
結果異常:
Exception in thread "main" net.sf.json.JSONException: JSONObject["details"] is not a JSONArray.
at net.sf.json.JSONObject.getJSONArray(JSONObject.java:2038)
at io..M.main(M.java:53)
3.解析已格式化xml
已格式化的xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<searchdetail totalRecords="1" pageSize="1" requestId="201910311451000013">
<details>
<transaction>
<player playerId="abc" partnerId="bcd"/>
<detail transactionId="43308923710" transactionDate="20191031 03:53:00" currency="CNY" game="FuStarH5" transactionSubType="Wager" handId="13809462143" amount="-2.50"/>
</transaction>
</details>
</searchdetail>
結果正常