Of a bug XMLSerializer

  The same code, and read the unformatted xml formatted xml, parsing unformatted given up, the code is very simple

1.java parsing code

    // 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. Parse unformatted xml

Unformatted 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>

Abnormal results:

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. formatted xml parsing

Formatted 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>

Normal results

Published 158 original articles · won praise 193 · Views 1.48 million +

Guess you like

Origin blog.csdn.net/zy_281870667/article/details/103161242