XML到JSON转换:为什么我的第一个XML标签被跳过?
在将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"
}
]
}
}
累积质地响应标签被跳过。
while converting XML to JSON my 1st XML tag into JSON is getting skipped
Here is the JAVA code snippet:
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);
here is the input:
<?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>
here is the output:
{
"TestCase": [
{
"Transactionid": "str1234",
"TransactionType": "str1234",
"Status": "str1234"
},
{
"Transactionid": "str5678",
"TransactionType": "str5678",
"Status": "str5678"
}
]
}
here is the required Output:
{
"AccumulateResponse": {
"TestCase": [
{
"Transactionid": "str1234",
"TransactionType": "str1234",
"Status": "str1234"
},
{
"Transactionid": "str5678",
"TransactionType": "str5678",
"Status": "str5678"
}
]
}
}
AccumulateResponse tag is getting skipped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有 underscore-java 带有方法
code> u.xmltojson(xml)
>。There is underscore-java library with method
U.xmlToJson(xml)
.