[SalesForce] Convert Apex XML response into JSON Format

This is my result in xml format how can i convert this into JSON format in salesforce .
(or) is there any other way directly deserialize the XML response.

MyResult == :

<?xml version="1.0" encoding="utf-8"?>
<results>
    <status code="ok"/>
    <my-events>
        <event sco-id="1290617895" type="event" icon="event" permission-id="host">
            <name>Adobe connect demo</name>
            <domain-name>meet64541292.adobeconnect.com</domain-name>
            <url-path>/demo/</url-path>
            <date-begin>2017-11-02T19:30:00.000+05:30</date-begin>
            <date-end>2017-11-03T18:45:00.000+05:30</date-end>
            <expired>true</expired>
            <duration>23:15:00.000</duration>
        </event>
        <event sco-id="1290640959" type="event" icon="event" permission-id="view">
            <name>salesforce demo</name>
            <domain-name>meet64541292.adobeconnect.com</domain-name>
            <url-path>/dmeosfsf/</url-path>
            <date-begin>2017-11-02T19:30:00.000+05:30</date-begin>
            <date-end>2017-11-02T19:45:00.000+05:30</date-end>
            <expired>true</expired>
            <duration>00:15:00.000</duration>
        </event>
        <event sco-id="1290643314" type="event" icon="event" permission-id="view">
            <name>Test</name>
            <domain-name>meet64541292.adobeconnect.com</domain-name>
            <url-path>/testadobe/</url-path>
            <date-begin>2017-11-02T18:45:00.000+05:30</date-begin>
            <date-end>2017-11-02T20:00:00.000+05:30</date-end>
            <expired>true</expired>
            <duration>01:15:00.000</duration>
        </event>
        <event sco-id="1292076280" type="event" icon="event" permission-id="view">
            <name>test1</name>
            <domain-name>meet64541292.adobeconnect.com</domain-name>
            <url-path>/test1/</url-path>
            <date-begin>2017-11-08T18:00:00.000+05:30</date-begin>
            <date-end>2017-11-08T19:00:00.000+05:30</date-end>
            <expired>true</expired>
            <duration>01:00:00.000</duration>
        </event>
        <event sco-id="1292084866" type="event" icon="event" permission-id="host">
            <name>TestEvent</name>
            <domain-name>meet64541292.adobeconnect.com</domain-name>
            <url-path>/testevent/</url-path>
            <date-begin>2017-11-08T17:45:00.000+05:30</date-begin>
            <date-end>2017-11-09T18:45:00.000+05:30</date-end>
            <expired>false</expired>
            <duration>1d 01:00:00.000</duration>
        </event>
    </my-events>
</results>

Best Answer

At this time, Salesforce does not provide a way to automatically transform XML to JSON (nor the other way around).

If you need to do something with XML, you need to write code specifically tailored to that domain (in other words, specific to the structure and data contained in the XML).

There are more than a few questions on this topic, and I'd suggest that you use the search bar on the top of the page to find them. I've even answered a few (1) of (2) them (3) myself (4)

The general approach I take is to use the Dom.Document and Dom.XmlNode classes in a series of classes that mimic the schema (structure) of the XML that you're dealing with. This is a 'divide and conquer' type approach. It's a bit of a pain to test, but it keeps the XML that you need to work with in any single method small and manageable.

To help you get started...

public class MyXmlParser{
    Results result;

    // In the top-level (outer) class, the constructor takes an XML document as a string
    //   and we start parsing it
    public MyXmlParser(String inputXML){
        Dom.Document xml = new Dom.Document();
        xml.load(inputXML);

        process(xml.getRootElement());
    }

    public void process(Dom.XmlNode inputNode){
        String currentNodeName;

        // This general pattern, looping over child nodes, creating class instances,
        //   and passing XML nodes to them will be repeated multiple times
        for(Dom.XmlNode childNode :inputNode.getChildElements){
            currentNodeName = childNode.getName();
            if(currentNodeName == 'results'){
                result = new Results();

                // Each level only concerns itself with extracting data on its own
                //   level.
                // If you need to call getChildElements() again to get more data, then
                //   in most cases, it's time to pass the baton on to an inner class.
                // This is what keeps the 'parsing' we're doing split up into
                //   manageable chunks
                result.process(childNode);
            }
        }
    }

    // Each XML tag that doesn't contain primitive data types gets its own inner class
    public class Results{
        Status stat;

        // If a tag only contains a collection of child tags, then it makes sense
        //   to make that a collection.
        // You could have another class for this one (which in turn would hold a
        //   collection of events) instead.
        // That's your call.
        List<Event> myEvents;

        // The constructor for each class is pretty basic, we just need to initialize
        //   collections (and not much else)
        public Results(){
            myEvents = new List<Event>();
        }

        // Each inner class gets a "process" method
        public void process(Dom.XmlNode inputNode){
            String currentNodeName;

            for(Dom.XmlNode childNode :inputNode.getChildElements()){
                currentNodeName = childNode.getName();
                if(currentNodeName == 'status'){
                    stat = new Status();
                    stat.process(childNode);
                }else if(currentNodeName == 'my-events'){
                    // Dealing with collections of xml nodes requires just a little change
                    //   to our approach.
                    // Instead of passing the 'my-events' node down, we need to extract
                    //   the children of that node, loop over them, and add the results
                    //   to our collection one at a time
                    Event tempEvent;
                    for(Dom.XmlNode grandchildNode :childNode.getChildElements()){
                        tempEvent = new Event();

                        // Note that we only go as far deep into the XML as is absolutely
                        //   necessary.
                        // Once we get to a place where we can pass the processing
                        //   off to another inner class, we do just that.
                        tempEvent.process(grandchildNode);
                        myEvents.add(tempEvent);
                    }
                }
            }
        }
    }

    public class Status{
        // Tag attributes become class variables
        String code;

        public void process(Dom.XmlNode inputNode){
            // Getting attributes from tags is pretty easy
            // The second parameter is the namespace of the tag, which you'll probably
            //   need to play around with yourself (until you can extract the attribute
            //   value)
            code = inputNode.getAttributeValue('code', '');
        }
    }

    public class Event{
        String sco_id;
        String type;
        String icon;
        String permission_id;

        // Tags that contain primitive data types (Strings, Integers, Booleans, Dates,
        //   etc...) also become class variables
        String name;
        String domain_name;
        // ...and so on

        public Event(){
        }

        public void process(Dom.XmlNode inputNode){
            // I'm skipping over extracting the attributes here, since I've already
            //   shown how that's done.
            String currentNodeName;

            for(Dom.XmlNode childNode :inputNode.getChildElements){
            currentNodeName = childNode.getName();
            if(currentNodeName == 'name'){
                // For nodes that contain a value only (no other nodes), we can
                //   extract the value using getText().
                // Strings don't require any special treatment
                name = childNode.getText();
            }else if(currentNodeName == 'date-begin'){
                // Non-string values (like this node, which is a datetime) will
                //   require some special handling
                // Most primitive types have a valueOf() method that allow you to
                //   pass a string in, and get an instance of the appropriate class out
                dateBegin = DateTime.valueOf(childNode.getText());
            }
        }
    }
}

The above code is only an example to get you started. It will not compile as-is, and you will need to complete it yourself. The point of what I wrote for you is to demonstrate everything you'll need to finish writing it yourself.