+ Start a Discussion
ashok  45ashok 45 

How to Convert(Serialize) XML into JSON format?

Hi All,
    This is my sample XML data. I want to convert(Serialize) XML into JSON. Iam doing like this If am wrong please let me know.
 String xml= ' <?xml version="1.0" encoding="utf-16"?>
 <GETmethod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Extend />
      <SampleData />
 String JSONString = JSON.serialize(xml);
    I want to post it into future method, like below. will it take as JSON format or String format?
 ==> CalloutMethod(JSONString)
  public static void CalloutMethod(JSONString)
You cannot just use the serialize method on your XML.  You will need to convert the XML to an object by parsing it [1] and then serializing the created object.

[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_xml_dom.htm