XML到JSON转换:为什么我的第一个XML标签被跳过?

发布于 2025-02-13 03:42:11 字数 1588 浏览 0 评论 0原文

在将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后eg是否自 2025-02-20 03:42:11

underscore-java 带有方法code> 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"
      }
    ]
  }
}

There is underscore-java library with method 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"
      }
    ]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文