简体   繁体   中英

Parse XML structure into JSON object string using Jackson

I need help parsing this XML message into a JSON structure. I have tried using Jackson library using XmlMapper . It keeps giving a parsing error. Do I need to parse out extra characters?

    <?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>

I have tried this:

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

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

But keep getting error

Misshaped close tag at 2299 [character 91 line 52]

The following works for me:

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);
}

Some things to try if it's still failing:

  • Try removing the processing instruction <?xml version="1.0" encoding="utf-8" standalone="yes"?> . Your code above seems to have leading spaces before this. Is that correct?
  • Check that the source XML passes validation.
  • Check for trailing characters in your source XML.

You can use JSONObject library to convert your xml to 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM