[SalesForce] XML Parsing using apex in salesforce

I want to do XML Parsing into apex. Please help me to do parsing in apex. see below XML Data.
Please explain me how to do xml parsing into apex to show this XML data into visualforce page and also insert this XML Data into custom object.

<Orders>
    <OrderID>3401</OrderID>
    <AddressValidated>Y</AddressValidated>
    <OrderDetails>
        <OrderDetailID>2584</OrderDetailID>
        <AutoDropShip>N</AutoDropShip>
        <FreeShippingItem>N</FreeShippingItem>
        <GiftTrakNumber>0</GiftTrakNumber>
        <GiftWrap/>
        <GiftWrapCost>0.0000</GiftWrapCost>
        <GiftWrapNote/>
        <ProductCode>800006</ProductCode>
        <ProductID>37506</ProductID>
        <ProductName>Wet & Forget Outdoor 1 Gallon</ProductName>
        <Shipped>Y</Shipped>
        <TotalPrice>34.9900</TotalPrice>
        <Warehouses>1</Warehouses>
    </OrderDetails>
</Orders>

Best Answer

This post from salesforce.com explains in detail with an example on how to work with XML data in apex.

Edit

I have just used the same code as provided in the link and it should work.

public class DomDocument {

    // Pass in the URL for the request
    // For the purposes of this sample,assume that the URL
    // returns the XML shown above in the response body
    public void parseResponseDom(String url){

       string xml = '<OrderDetails>
                            <OrderDetailID>2584</OrderDetailID>
                            <GiftWrapCost>0.0000</GiftWrapCost>
                            <GiftWrapNote/>
                            <ProductCode>800006</ProductCode> 
                           </OrderDetails>';
       Dom.Document doc = new Dom.Document();
       doc.load(xml);

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

        String OrderDetailId= ordDtls.getChildElement('OrderDetailId', null).getText();
        String prdCode = ordDtls.getChildElement('ProductCode', null).getText();
        // print out specific elements
        System.debug('OrderDtlID: ' + OrderDetailId);
        System.debug('Prod Code: ' + prdCode );

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