使用Jackson将XML结构解析为JSON对象字符串

时间:2018-03-17 21:59:17

标签: java json xml jackson

我需要帮助将此XML消息解析为JSON结构。我尝试使用XmlMapper使用杰克逊库。它不断给出解析错误。我是否需要解析额外的字符?

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns2:GetLineItemDetailResponse xmlns:ns2="http://schema.tmt.com/transactionlineitemdetail/loyalty/v1">
    <GetLineItemDetailResult>
        <Coupons>
            <Coupon>
                <CouponType>sim_default_CouponType</CouponType>
                <CouponNumber>sim_default_CouponNumber</CouponNumber>
                <LineNumber>0</LineNumber>
                <Amount>0</Amount>
            </Coupon>
        </Coupons>
        <Tenders>
            <Tender>
                <TenderCode>sim_default_TenderCode</TenderCode>
                <Amount>0</Amount>
                <RedemptionID>0</RedemptionID>
            </Tender>
        </Tenders>
        <LineItems>
            <LineItem>
                <LineNumber>0</LineNumber>
                <Type>1</Type>
                <Quantity>0</Quantity>
                <TotalTax>0</TotalTax>
                <UPC>sim_default_UPC</UPC>
                <UnitPrice>0</UnitPrice>
                <ExtendedPrice>0</ExtendedPrice>
                <TotalPrice>0</TotalPrice>
                <OriginalRFN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                <OriginalOrderID xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                <IneligForRedeem>false</IneligForRedeem>
                <RX>
                    <Days>0</Days>
                    <ThirtyTo90>false</ThirtyTo90>
                    <GovtFunded>1970-01-01T00:00:00</GovtFunded>
                    <RXType>NEW</RXType>
                    <PartialFill>false</PartialFill>
                    <ImmunInd>false</ImmunInd>
                    <ImmunType>sim_default_ImmunType</ImmunType>
                    <RXEligibleInd>false</RXEligibleInd>
                </RX>
                <ns3:UPCDescription xmlns:ns3="http://www.epsilon.com/webservices/">sim_default_UPCDescription</ns3:UPCDescription>
            </LineItem>
        </LineItems>
        <Redemption>
            <AwardID>0</AwardID>
            <Amount>0</Amount>
            <Points>0</Points>
        </Redemption>
    </GetLineItemDetailResult>
</ns2:GetLineItemDetailResponse>

我试过这个:

   XmlMapper xmlMapper = new XmlMapper();
   JsonNode node = xmlMapper.readTree(xml.getBytes());

   ObjectMapper jsonMapper = new ObjectMapper();
   String json = jsonMapper.writeValueAsString(node);
   System.out.println(json);

但不断收到错误

Misshaped close tag at 2299 [character 91 line 52]

2 个答案:

答案 0 :(得分:0)

以下适用于我:

public static void main(String[] args) throws IOException {

    String xml = new String(Files.readAllBytes(Paths.get(".... /xmlfile.xml")));

    XmlMapper xmlMapper = new XmlMapper();
    JsonNode node = xmlMapper.readTree(xml.getBytes());

    ObjectMapper jsonMapper = new ObjectMapper();
    String json = jsonMapper.writeValueAsString(node);

    System.out.println(json);
}

如果它仍然失败,可以尝试一些事情:

  • 尝试删除处理说明<?xml version="1.0" encoding="utf-8" standalone="yes"?>。上面的代码在此之前似乎有前导空格。这是对的吗?
  • 检查源XML是否通过验证。
  • 检查源XML中的尾随字符。

答案 1 :(得分:0)

您可以使用JSONObject库将xml转换为JSON。

  String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n" +
                "<ns2:GetLineItemDetailResponse xmlns:ns2=\"http://schema.tmt.com/transactionlineitemdetail/loyalty/v1\">\n" +
                "    <GetLineItemDetailResult>\n" +
                "        <Coupons>\n" +
                "            <Coupon>\n" +
                "                <CouponType>sim_default_CouponType</CouponType>\n" +
                "                <CouponNumber>sim_default_CouponNumber</CouponNumber>\n" +
                "                <LineNumber>0</LineNumber>\n" +
                "                <Amount>0</Amount>\n" +
                "            </Coupon>\n" +
                "        </Coupons>\n" +
                "        <Tenders>\n" +
                "            <Tender>\n" +
                "                <TenderCode>sim_default_TenderCode</TenderCode>\n" +
                "                <Amount>0</Amount>\n" +
                "                <RedemptionID>0</RedemptionID>\n" +
                "            </Tender>\n" +
                "        </Tenders>\n" +
                "        <LineItems>\n" +
                "            <LineItem>\n" +
                "                <LineNumber>0</LineNumber>\n" +
                "                <Type>1</Type>\n" +
                "                <Quantity>0</Quantity>\n" +
                "                <TotalTax>0</TotalTax>\n" +
                "                <UPC>sim_default_UPC</UPC>\n" +
                "                <UnitPrice>0</UnitPrice>\n" +
                "                <ExtendedPrice>0</ExtendedPrice>\n" +
                "                <TotalPrice>0</TotalPrice>\n" +
                "                <OriginalRFN xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>\n" +
                "                <OriginalOrderID xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>\n" +
                "                <IneligForRedeem>false</IneligForRedeem>\n" +
                "                <RX>\n" +
                "                    <Days>0</Days>\n" +
                "                    <ThirtyTo90>false</ThirtyTo90>\n" +
                "                    <GovtFunded>1970-01-01T00:00:00</GovtFunded>\n" +
                "                    <RXType>NEW</RXType>\n" +
                "                    <PartialFill>false</PartialFill>\n" +
                "                    <ImmunInd>false</ImmunInd>\n" +
                "                    <ImmunType>sim_default_ImmunType</ImmunType>\n" +
                "                    <RXEligibleInd>false</RXEligibleInd>\n" +
                "                </RX>\n" +
                "                <ns3:UPCDescription xmlns:ns3=\"http://www.epsilon.com/webservices/\">sim_default_UPCDescription</ns3:UPCDescription>\n" +
                "            </LineItem>\n" +
                "        </LineItems>\n" +
                "        <Redemption>\n" +
                "            <AwardID>0</AwardID>\n" +
                "            <Amount>0</Amount>\n" +
                "            <Points>0</Points>\n" +
                "        </Redemption>\n" +
                "    </GetLineItemDetailResult>\n" +
                "</ns2:GetLineItemDetailResponse>";

        System.out.println(xml);
        JSONObject obj = XML.toJSONObject(xml);
        System.out.println(obj);