Skip to main content
1 of 2
ipradhansk
  • 2.3k
  • 4
  • 27
  • 51

If you are planning to use XML, and parse it in Apex using DOM, then you could use following

public void parseData(String xmlString){ //Pass XML String

   /* e.g., xmlString = '<ObjectName>
                          <Attribute1>Value1</Attribute1>
                          <Attribute2>Value2</Attribute2>
                        </ObjectName>'; */
   Dom.Document doc = new Dom.Document();
   doc.load(xmlString);

    //Retrieve the root element for this document.
    Dom.XMLNode details = doc.getRootElement();

    String attr1 = details.getChildElement('Attribute1', null).getText();
    String attr2 = details.getChildElement('Attribute2', null).getText();
    
    // print out specific elements
    System.debug('Attribute1: ' + Attribute1);
    System.debug('Attribute2: ' + Attribute2);

    // Alternatively, loop through the child elements.
    // This prints out all the elements of the address
    for(Dom.XMLNode child : details.getChildElements()) {
       System.debug(child.getText());
    }
}

Referrences: here and here

ipradhansk
  • 2.3k
  • 4
  • 27
  • 51