0

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]
6
  • Possible duplicate of How to convert XML to JSON using only Jackson? Commented Mar 17, 2018 at 22:34
  • What is the error you're seeing? Commented Mar 17, 2018 at 22:38
  • I have tried the solutions on that but they didnt work. Commented Mar 17, 2018 at 22:38
  • Misshaped close tag at 2299 [character 91 line 52] Commented Mar 17, 2018 at 22:40
  • It works for me when I read the XML in from a file. There's only 50 lines of XML. How are your reading the XML into your program? Commented Mar 17, 2018 at 22:56

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.