How to parse an XML file and save the parsed data into the corresponding objects?

| By Webner


First of all, we need some dummy XML data for parsing as given below:
Demo XML

The above XML has many tags. The first one being the XML tag which lets us know that it is an XML document. Moving on to the next one we have Vehicles tag which is the root tag of our XML object, it is followed by various vehicle tags which let us know that we have an array of vehicles.

Now to parse this XML we need to write some code in APEX class as follows:

public class DemoXMLParse{
    public list vehicles = new list();
    public Vehicle__c vehicle;
    public DemoXMLParse(){
        String XMLString =  'HondaCity20
	//create an object of DOM.Document and load the xml into it
            DOM.Document doc=new DOM.Document();
	//get root element of the XML
            DOM.XmlNode rootNode=doc.getRootElement();
            DOM.XmlNode [] all= rootNode.getchildelements();

            for(Dom.XMLNode child : all) {
                vehicle = new Vehicle__c();
	for (Integer i = 0; i < child.getAttributeCount(); i ++) { 
if (child.getAttributeLocalName(i).equals('vin')) value = child.getAttributeValueAt(i);
 	 for(Dom.XmlNode c2 : child.getchildelements()) {
                            vehicle.Make__c = c2.gettext();
                            vehicle.Model__c = c2.gettext();
                            vehicle.Model_Year__c = c2.gettext();
                }//for loop for vehicle
	insert vehicles;
        } catch(exception e){ system.debug(e.getMessage()); }//catch

DOM.Document(): Use Document class to parse XML content. Create a new instance of the Dom.Document class.

getRootElement(): It is used to get the top-level root element in the XML. It returns null if it does not find any.

getchildelements(): It returns all the child element nodes for this node. It does not include child text or comment nodes.

getChildren(): It returns all the child elements and it includes all the nodes types.

getAttributeValue(key, keyNamespace): Returns the attribute value for the given key and keyNamespace.

In the above apex class, we have created an object of DOM. Document and then with the help of it we then loaded our XML as a string and then get the root node of our XML. Then getchildelements function of DOM.XML Node will generate an array of vehicle objects under the vehicles root node.

After that, we just have to get the desired elements used for the loop and getchildelements function and insert the data into the vehicle object according to the corresponding fields. Then just put the vehicle object into vehicles list and simply insert the list into the custom created vehicles object.

Then the output of the above-written code will insert two objects in the vehicles.

Now if we open one of the vehicle objects, it is showing the Vehicle Name as auto-generated i.d, Make, Model and Model Year as shown below.

Leave a Reply

Your email address will not be published. Required fields are marked *