我是JAVA的新手,并尝试将JSON数据格式转换为XML以获取多个记录。结果不是一个定义良好的XML结构。当存在一个项目记录时,下面的代码工作正常。
{
"items": [
{
"item": {
"ID": "79C675F752491945E1A",
"Subject": "test1 ",
"Sender": "user1",
"Date": "2014-07-14 14:14:40",
"DocumentClassID": "1",
"Comment": "",
}
},
{
"item": {
"ID": "79C67761945E1A",
"Subject": "test2",
"Sender": "user2",
"Date": "2014-07-14 14:14:40",
"DocumentClassID": "1",
"Comment": "",
}
}
]
}
=================== Code Snippet ========================== ===
public class ConvJsonXml extends AbstractTransformation {
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
try {
String sourcexml = ""; String targetxml =""; String line ="";
InputStream ins = in.getInputPayload().getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(ins));
while ((line = br.readLine()) != null)
sourcexml +=line+"\n";
br.close();
JSONObject o = new JSONObject(sourcexml);
targetxml = org.json.XML.toString(o);
out.getOutputPayload().getOutputStream().write(targetxml.getBytes());
}
catch (Exception e) {
throw new StreamTransformationException(e.getMessage());
}
}
}
请建议我在Try块中添加代码,以便转换提供一个统一的XML结构。
此致..