function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nayana PawarNayana Pawar 

XML Parsing

Hi All,
I want to do XML Parsing into apex. Please help me to do parsing in apex.
<Orders>
    <OrderID>3401</OrderID>
    <AddressValidated>Y</AddressValidated>

    <OrderDetails>
        <OrderDetailID>2584</OrderDetailID>
        <GiftWrapCost>0.0000</GiftWrapCost>
        <GiftWrapNote/>
        <ProductCode>800006</ProductCode>
        
    </OrderDetails>
</Orders>
pconpcon
You can do this with the Dom.Document object
 
String xmlData = '<Orders>' +
        '<OrderID>3401</OrderID>' +
        '<AddressValidated>Y</AddressValidated>' +
        '<OrderDetails>' +
            '<OrderDetailID>2584</OrderDetailID>' +
            '<GiftWrapCost>0.0000</GiftWrapCost>' + 
            '<GiftWrapNote/>' +
            '<ProductCode>800006</ProductCode>' +
        '</OrderDetails>'+
    '</Orders>';
Dom.Document doc = new Dom.Document();
doc.load(xmlData);
Dom.XmlNode order = doc.getRootElement();
String orderId = order.getChildElement('OrderID', null).getText();
Dom.XmlNode orderDetails = order.getChildElement('OrderDetails', null);
String giftWrapCost = orderDetails.getChildElement('GiftWrapCost', null).getText();

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_xml_dom.htm