下面的 json 使用 json-lib 实用程序转换为 xml。
{
"tms:getTaskListResponse": {
"tms:task": [
{
"tms:creationDate": "2013-05-06T12:02:21.530+05:30",
"tms:instanceId": "403"
},
{
"tms:creationDate": "2013-05-06T12:04:50.730+05:30",
"tms:instanceId": "1224"
}
]
}
}
使用的代码是:
XMLSerializer serializer = new XMLSerializer();
serializer.setRootName("root");
serializer.setTypeHintsEnabled(false);
JSON json = JSONSerializer.toJSON(jsonString);
String xmlResponse = serializer.write(json);
该<root>
标签已被删除。删除 OUTPUT XML 后:
<?xml version="1.0" encoding="UTF-8"?>
<tms:getTaskListResponse >
<tms:task>
<e>
<tms:creationDate>2013-05-06T12:02:21.530+05:30</tms:creationDate>
<tms:instanceId>403</tms:instanceId>
</e>
<e>
<tms:creationDate>2013-05-06T12:04:50.730+05:30</tms:creationDate>
<tms:instanceId>1224</tms:instanceId>
</e>
</tms:task>
</tms:getTaskListResponse>
预期格式:
<?xml version="1.0" encoding="UTF-8"?>
<tms:getTaskListResponse >
<tms:task>
<tms:creationDate>2013-05-06T12:02:21.530+05:30</tms:creationDate>
<tms:instanceId>403</tms:instanceId>
</tms:task>
<tms:task>
<tms:creationDate>2013-05-06T12:04:50.730+05:30</tms:creationDate>
<tms:instanceId>1224</tms:instanceId>
</tms:task>
</tms:getTaskListResponse>
有没有办法以预期的 xml 格式调整 xml 转换?