Open greenlaw110 opened 3 years ago
The introducing of #240 brought in an error that it cannot handle simple XML without attributes conversion correctly.
This CR is to improve XML to JSON convert logic to handle XML with or without attributes correctly.
<root> <parent> <child>123</child> </parent> </root>
converted ($.convert(xmlDoc).to(JSONObject.class)) into
$.convert(xmlDoc).to(JSONObject.class)
{ "parent": { "child": 123 } }
<root> <parent> <child>123</child> </parent> <parent> <child>456</child> </parent> </root>
converted into
JSON without MERGE_MAP hint:
{ "parent": [ { "child": 123 }, { "child": 456 } ] }
JSON with MERGE_MAP hint ($.convert(xmlDoc).hint(XmlToJson.HINT_MERGE_MAP).to(JSONObject.class)) turned on:
$.convert(xmlDoc).hint(XmlToJson.HINT_MERGE_MAP).to(JSONObject.class)
{ "parent": { "child": [123, 456] } }
<root> <parent> <child>123</child> </parent> <parent> <child>456</child> </parent> <parent2> <child>789</child> </parent2> <someone>abc</someone> </root>
{ "parent": [ { "child": 123 }, { "child": 456 } ], "parent2": { "child": 789 }, "someone": "abc" }
JSON with MERGE_MAP hint turned on
{ "parent": { "child": [123, 456] }, "parent2": { "child": 789 }, "someone": "abc" }
<root> <parent> <child value="123"/> </parent> </root>
{ "parent": { "child": { "value": 123 } } }
<root> <parent> <child value="123">abc</child> </parent> </root>
{ "parent": { "child": { "value": 123, "innerValue": "abc" } } }
OSGL tool XML to JSON convert tool require at least one ELEMENT type node inside the root node, otherwise it returns an empty JSONObject.
E.g. converting the following xml document will return an empty JSONObject:
<root>abc</root>
Refer: https://github.com/osglworks/java-tool/blob/master/src/test/java/org/osgl/util/XMLTest.java#L32
The introducing of #240 brought in an error that it cannot handle simple XML without attributes conversion correctly.
This CR is to improve XML to JSON convert logic to handle XML with or without attributes correctly.
Case 1.1 - simple XML without attributes
converted (
$.convert(xmlDoc).to(JSONObject.class)
) intoCase 1.2 - simple XML list without attributes
converted into
JSON without MERGE_MAP hint:
JSON with MERGE_MAP hint (
$.convert(xmlDoc).hint(XmlToJson.HINT_MERGE_MAP).to(JSONObject.class)
) turned on:Case 1.3 - simple mixed XML list without attributes
converted into
JSON without MERGE_MAP hint:
JSON with MERGE_MAP hint turned on
Case 2.1 - XML with attributes and no text
converted into
Case three - XML with attributes and value
converted into
Note
OSGL tool XML to JSON convert tool require at least one ELEMENT type node inside the root node, otherwise it returns an empty JSONObject.
E.g. converting the following xml document will return an empty JSONObject: