Concepts or coding lessons of Salesforce that you can implement easily

Easily Parse XML File Apex In Salesforce Using DOM.Document class

In our Salesforce career, at some point we have business use case to Parse XML File APEX in Salesforce.

In this blog, I am going to explain you a Simple way to generate XML, parse and retrieve values from XML.

Apex provides classes that enable you to work with XML content using the DOM 
(Document Object Model).

Many Different platforms give us their own way of generating and parsing XML, and likewise APEX is no exception. 

Use Case:


We usually get a requirement to store Salesforce records in XML format. Later, we may need to parse the XML and retrieve the data from XML and store them into a Map for future use.

Solution:



I have created XMLParserClass to generate some hard coded data. Later through usable parseXML method that has been parsed.

Output:


Now its time to check our code output:

Run below code snippet through developer console:

XMLParserClass obj = new XMLParserClass();
String xmlText = obj.generateXML();
obj.parserXML(xmlText );
System.debug(obj.xmlDataMap);

Done, Thats it 👍 !!!
loading...

1 comment: