在将XML转换为JSON时,我的第一个XML标记被跳过
下面是JAVA代码片段:String data="";
data = FileUtils.readFileToString(new File("src/main/resources/student.xml"), "UTF-8");
XmlMapper xmlMapper = new XmlMapper();
JsonNode jsonNode = xmlMapper.readTree(data.getBytes());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writerWithDefaultPrettyPrinter().writeValue(newFile("src/main/resources/student.json"),jsonNode);
这里是输入:
<?xml version="1.0" encoding="utf-8"?>
<AccumulateResponse>
<TestCase>
<Transactionid>str1234</Transactionid>
<TransactionType>str1234</TransactionType>
<Status>str1234</Status>
</TestCase>
<TestCase>
<Transactionid>str5678</Transactionid>
<TransactionType>str5678</TransactionType>
<Status>str5678</Status>
</TestCase>
</AccumulateResponse>
输出如下:
{
"TestCase": [
{
"Transactionid": "str1234",
"TransactionType": "str1234",
"Status": "str1234"
},
{
"Transactionid": "str5678",
"TransactionType": "str5678",
"Status": "str5678"
}
]
}
这里是需要的输出:
{
"AccumulateResponse": {
"TestCase": [
{
"Transactionid": "str1234",
"TransactionType": "str1234",
"Status": "str1234"
},
{
"Transactionid": "str5678",
"TransactionType": "str5678",
"Status": "str5678"
}
]
}
}
accumulaterresponse标签被跳过。
有一个带下划线的java库,方法是U.xmlToJson(xml)
。
<?xml version="1.0" encoding="utf-8"?>
<AccumulateResponse>
<TestCase>
<Transactionid>str1234</Transactionid>
<TransactionType>str1234</TransactionType>
<Status>str1234</Status>
</TestCase>
<TestCase>
<Transactionid>str5678</Transactionid>
<TransactionType>str5678</TransactionType>
<Status>str5678</Status>
</TestCase>
</AccumulateResponse>
{
"AccumulateResponse": {
"TestCase": [
{
"Transactionid": "str1234",
"TransactionType": "str1234",
"Status": "str1234"
},
{
"Transactionid": "str5678",
"TransactionType": "str5678",
"Status": "str5678"
}
]
}
}