0

I want to convert XML String to a JSON object.

Below is the XML:

<request>
    <Products>
        <Product>
            <ProductName>H &amp; M</ProductName>
            <quantity>1</quantity>
            <totalProductCost>17.03</totalProductCost>
        </Product>
    </Products>
</request>

This XML string comes as a request parameter. httpReqMap is the map containing request parameters and their values. When I execute this statement

String reqstring = httpReqMap.get("request");

breaks the XML string in 2 parts. Thus the XML is invalid and I am unable to convert it to JSON Object.

I have tried to pass '&' as '&amp;'.

Below is the code:

String reqstring = httpReqMap.get("request");
JSONObject jsonObject = XML.toJSONObject(httpReqMap.get("request"));

It should escape special character and convert XML to JSON.

SirFartALot
  • 1,215
  • 5
  • 25
Surabhi
  • 37
  • 5

1 Answers1

0

I think your best option is to pass the XML as the payload of a POST query, not in the URL.

If you really want to do that, you must url-encode the whole xml. There is a number of questions on this subject, for example Java URL encoding of query string parameters.

luca.vercelli
  • 898
  • 7
  • 24