How to Parse XML data in Apex

by Rijwan Mohmmed
how-to-parse-xml-data-in-apex-techdicer

Hello friends, today we are going to discuss How to Parse xml data in Apex Salesforce. Many times we get the chance that you must have used XML.

Also check this: Generate Public URL for Salesforce Files

If you are a web developer then chances are that you must have used XML. In fact, even non developers have heard about it. Data has been stored, sent and received in this format in the software industry for decades. While other formats such as JSON are taking over for getting the same job done, XML is still the best method for many API responses. To store data, we can easily generate and parse an XML file.  

Key Highlights :

  1. XML is use for API response and request.
  2. Apex provides classes that enable you to work with XML content using the DOM (Document Object Model).
  3. Use the XmlNode Class to work with a node in the XML document.

Let’s see how APEX predefined classes can be used to parse XML.

Process :

Step 1: First of all we need dummy XML data

<Results>
	<Account>
		<UserId> userId</UserId>
		<Active> true </Active>
	</Account>
	<Quotes>
		<Company>
			<Name>Test1</Name>
			<Type>IT</Type>
		</Company>
		<Company>
			<Name>Test12</Name>
			<Type>Farm</Type>
		</Company>
	</Quotes>
</Results>

Each array has a child node, a name and a code. Each child node corresponds to the label of the data that it carries, while the data filled in each tag corresponds to the data in that product array. Now we come to the actual data, which is stored in the tags.

Step 2 : Now Comes on APEX Class , this is tricky but I write such way that you can understand easily.

TechdicerXMLParse.cls :

public class TechdicerXMLParse {
    
    public static void parseXMl(){
        String strResp = '<?xml version="1.0" encoding="UTF-8"?><Results><Account><UserId> userId</UserId><Active> true </Active></Account><Quotes><Company><Name>Test1</Name><Type>IT</Type></Company><Company><Name>Test12</Name><Type>Farm</Type></Company></Quotes></Results>';
        Dom.Document doc = new Dom.Document();  
        doc.load(strResp);  
        Dom.XMLNode rootElement = doc.getRootElement();  
        for ( Dom.XMLNode childElement : rootElement.getChildElements() ) {
            //root elements of each array
            system.debug(childElement.getName() + '-' + childElement.getText());
            for (Dom.XMLNode detailElement : childElement.getChildElements()){
                //childs elements of each array
                system.debug(detailElement.getName() + '-' + detailElement.getText());
                
                for (Dom.XMLNode detailElement1 : detailElement.getChildElements()){
                    //childs elements of each childs array
                    system.debug(detailElement1.getName() + '-' + detailElement1.getText());
                }
            } 
        }  
    }
}

Output :

how-to-parse-xml-data-in-apex-output-techdicer
how-to-parse-xml-data-in-apex-output-techdicer

Reference :

  1. XML in Salesforce
What’s your Reaction?
+1
2
+1
0
+1
0
+1
0
+1
0
+1
2

You may also like

Leave a Comment