0
<Layout xmlns="http://soap.sforce.com/2006/04/metadata">
    <excludeButtons>Submit</excludeButtons>
    <layoutSections>
        <customLabel>false</customLabel>
        <detailHeading>false</detailHeading>
        <editHeading>true</editHeading>
        <label>Information</label>
        <layoutColumns>
            <layoutItems>
                <behavior>Readonly</behavior>
                <field>Hyperlink__c</field>
            </layoutItems>
        </layoutColumns>
        <layoutColumns/>
        <style>TwoColumnsTopToBottom</style>
    </layoutSections>
    <layoutSections>
        <customLabel>false</customLabel>
        <detailHeading>false</detailHeading>
        <editHeading>true</editHeading>
        <label>ABC</label>
        <layoutColumns>
            <layoutItems>
                <behavior>Readonly</behavior>
                <field>TOTAL__c</field>
            </layoutItems>
            <layoutItems>
                <behavior>Readonly</behavior>
                <field>SUBTOTAL__c</field>
            </layoutItems>
        </layoutColumns>
        <layoutColumns/>
        <style>TwoColumnsTopToBottom</style>
    </layoutSections>
</Layout>                                                                                              
                                                                                                  

Above code is input as a xml file now I want to convert this code in to given below output. output:Information - Hyperlink__c ABC - TOTAL__c, SUBTOTAL__c

please any one help me, its important to me. Thanks. 
parvathi
  • 1
  • 1
  • Hi parvathi. What have you tried so far? Do you have any sample code? Where do you want this output displayed? More details would help us give you an accurate solution. – Arepa Slayer Jan 19 '22 at 20:07

2 Answers2

0

I would start by assigning the input xml to an XML Dom Document, for example:

Dom.Document doc = new Dom.Document();
doc.load(xml);

Then, you can access its attributes like this:

Dom.XMLNode layout = doc.getRootElement();
Dom.XmlNode[] layoutSections = layout.getChildElements();

Once you have access to the layout sections, you can loop through them and access specific child elements with:

String label = layoutSection.getChildElement('label', null).getText();

Note that a layoutSection is of type XmlNode which means you can keep going through its child elements and access its values.

JoseStack
  • 167
  • 2
  • 12
0

The following example uses a recursive algorithm to parse the XML stream and convert it to a Map<String, Object> format.

public class TestSwapnil {
    
    //method to initiate recursive parsing of XML stream
    public static Map<String, Object> xmlToMap(String xml){
        
        //initialzing XML document
        Dom.Document doc = new Dom.Document();
        doc.load(xml);
        
        //getting the root element and parsing it recursively
        Dom.XMLNode root = doc.getRootElement();
        Object temp = parse(root);
        
        //map to store parsed data
        Map<String, Object> jsonResult = new Map<String, Object>{root.getName() => temp};
        
        //result map having data from the XML string as a map
        return jsonResult;
        
    }
    
    //method to recursively parse child nodes from XML document
    private static Object parse(Dom.XMLNode node){
        
        //map to store data of current XML node
        Map<String, Object> jsonResult = new Map<String, Object>();
        
        //getting list of child elements
        List<Dom.XMLNode> children = node.getChildElements();
        
        //if no child elements found, we simply return the text value
        if(children.isEmpty()){
            return node.getText();
        }

        //map to store occurence count of child elements against their names       
        Map<String, Integer> nodeNameCount = new Map<String, Integer>();
        
        //iterating over child elements and populating the count in map
        for(Dom.XMLNode child : children){
            
            String nodeName = child.getName();
            
            if(!nodeNameCount.containsKey(child.getName())){
                nodeNameCount.put(nodeName, 0);
            }

            Integer oldCount = nodeNameCount.get(nodeName);
            nodeNameCount.put(nodeName, oldCount + 1);
            
        }
        
        //iterating over child elements and parsing them recursively
        for(Dom.XMLNode child : children) {
        
            Object temp = parse(child);
            String nodeName = child.getName();
            
            //checking if this child is an array 
            Boolean childIsArray = (nodeNameCount.get(nodeName) > 1);
            
            //if child is array, save the values as array, else as strings. 
            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, temp);
            }
        }
        
        //result map for current node
        return jsonResult;
        
    }

}

I have used the following block of code to test the above methods in Anonymous window :

//input xml data
String xml = '<book><title>Some title</title><description>some description</description><author><id>1</id><name>some author name</name></author><review>nice book</review><review>this book sucks</review><review>amazing work</review></book>';

//parsing xml data and printing to debug logs
Map<String, Object> jsonResult = TestSwapnil.xmlToMap(xml);
System.debug(jsonResult);

Output in Debug logs :

22:57:28:007 VARIABLE_ASSIGNMENT [15]|jsonResult|

{
  "book": {
    "title": "Some title",
    "description": "some description",
    "author": {
      "id": "1",
      "name": "some author name"
    },
    "review": [
      "nice book",
      "this book sucks",
      "amazing work"
    ]
  }
}

|0x698881dd