How to Parse XML from Static Resource using Apex in Salesforce?

How to Parse XML from Static Resource using Apex in Salesforce?

Sample XML Code:
<message>
<to>Magulan</to>
<from>InfallibleTechie</from>
<title>Welcome</title>
<body>Sample Message</body>
</message>

XML Static Resource:


Sample Apex Code:
StaticResource objSR = [
    SELECT ID, Body
    FROM StaticResource
    WHERE Name = ‘Testing’
    LIMIT 1
];
String strResp = objSR.Body.toString();
System.debug( ‘Static Resource body is ‘ + strResp );
Dom.Document doc = new Dom.Document();  
doc.load( strResp );  
Dom.XMLNode rootElement = doc.getRootElement();

for ( Dom.XMLNode childElement : rootElement.getChildElements() ) {  

    System.debug( childElement.getName() + ‘ – ‘ + childElement.getText() );
      

 
Output:

Leave a Reply