Home

Wednesday, 15 May 2024

sfdc-learn #12 : XML To JSON Response Parsing

 XML Parsing in Apex

Read the response stream as a XML document using DOM.XmlNode class.


public class XmlToJsonConvert {

public static Map<String,Object> xmlToMap(String xml){

        //Intializing XML Document

        Dom.document doc = new Dom.Document();

        doc.load(xml);

       

        //Getting root element and parse it recursively

        Dom.XMLNode root = doc.getRootElement();

        Object temp = parse(root);

        

        //final map to store data

        Map<String,Object> finalJsonResult = new Map<String,Object> {root.getName() => temp}; 

        return finalJsonResult;  

    }

    

    public static Object parse(Dom.XMLNode node){

        

         Map<String,Object> nodeNameCount = new Map<String,Object>();

         Map<String, List<Object>> jsonResult =  new  Map<String, List<Object>>();

        

        // get child element

        List<Dom.XMLNode> childrens = node.getChildElements();

        

        // if it is emty then retun text value

        if(childrens.isEmpty()){

            return node.getText();

        }

        

        // Create map to store child ocuurence count

        Map<String,Integer> nodeNameCountMap = new Map<String,Integer>();

        

        // itegrate child elemets and make a map

        for(Dom.XMLNode child : childrens){

            String nodeName = child.getName();

            

            if(!nodeNameCountMap.containsKey(nodeName)){

                nodeNameCountMap.put(nodeName,0);

            }

            

            Integer oldCound = nodeNameCountMap.get(nodeName);

            nodeNameCountMap.put(nodeName, oldCound + 1);

        }

        

         //iterating over child elements and parsing them recursively

        for(Dom.XMLNode child : childrens) {

            

            Object temp = parse(child);

            String nodeName = child.getName();

            // Check child is an array

            Boolean childIsArray = ( nodeNameCountMap.get(nodeName) > 1);

            

            //if array then save value in list else string

            if(childIsArray){

                if(jsonResult.get(nodeName) == null){

                    jsonResult.put(nodeName,new List<Object>{temp});   

                }else{

                    List<Object> tempList = (List<Object>) jsonResult.get(nodeName);

                    tempList.add(temp);

                    jsonResult.put(nodeName, tempList); 

                }

                

            }else{

                jsonResult.put(nodeName,new List<Object>{temp});

            }

        } 

        return jsonResult;

    }

}


//input xml data

String xml = '<customers><customer id="55000"><name>Test Cust</name><address><street>100 Main</street><city>Pune</city><state>MH</state><zip>01701</zip></address><address><street>720 Prospect</street><city>Nashik</city><state>MH</state><zip>01701</zip></address></customer></customers>';


Map<String, Object> jsonResult = XmlToJsonConvert.xmlToMap(xml);

System.debug(jsonResult);

System.debug(Json.serialize(jsonResult));


Debug Log:



No comments:

Post a Comment