How to parse XML string in Salesforce using Apex?

Sample XML:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <breakfast_menu>  
  3.   <food>  
  4.     <name>Belgian Waffles</name>  
  5.     <price>$5.95</price>  
  6.     <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>  
  7.     <calories>650</calories>  
  8.   </food>  
  9.   <food>  
  10.     <name>Strawberry Belgian Waffles</name>  
  11.     <price>$7.95</price>  
  12.     <description>Light Belgian waffles covered with strawberries and whipped cream</description>  
  13.     <calories>900</calories>  
  14.   </food>  
  15.   <food>  
  16.     <name>Berry-Berry Belgian Waffles</name>  
  17.     <price>$8.95</price>  
  18.     <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>  
  19.     <calories>900</calories>  
  20.   </food>  
  21.   <food>  
  22.     <name>French Toast</name>  
  23.     <price>$4.50</price>  
  24.     <description>Thick slices made from our homemade sourdough bread</description>  
  25.     <calories>600</calories>  
  26.   </food>  
  27.   <food>  
  28.     <name>Homestyle Breakfast</name>  
  29.     <price>$6.95</price>  
  30.     <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>  
  31.     <calories>950</calories>  
  32.   </food>  
  33. </breakfast_menu>  

Sample Apex Code to Parse:

  1. String strResp = '<?xml version="1.0" encoding="UTF-8"?><breakfast_menu>  <food>    <name>Belgian Waffles</name>    <price>$5.95</price>    <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>    <calories>650</calories>  </food>  <food>    <name>Strawberry Belgian Waffles</name>    <price>$7.95</price>    <description>Light Belgian waffles covered with strawberries and whipped cream</description>    <calories>900</calories>  </food>  <food>    <name>Berry-Berry Belgian Waffles</name>    <price>$8.95</price>    <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>    <calories>900</calories>  </food>  <food>    <name>French Toast</name>    <price>$4.50</price>    <description>Thick slices made from our homemade sourdough bread</description>    <calories>600</calories>  </food>  <food>    <name>Homestyle Breakfast</name>    <price>$6.95</price>    <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>    <calories>950</calories>  </food></breakfast_menu>';  
  2. Dom.Document doc = new Dom.Document();  
  3. doc.load( strResp );  
  4. Dom.XMLNode rootElement = doc.getRootElement();  
  5. for ( Dom.XMLNode childElement : rootElement.getChildElements() ) {  
  6.       
  7.     for ( Dom.XMLNode detailElement : childElement.getChildElements() )  
  8.         system.debug( detailElement.getName() + '-' + detailElement.getText() );  
  9.       
  10. }  

Output:


No comments:

Post a Comment