0

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>
1
  • Nayana -- normal SFSE practice is to demonstrate that you have tried something -- there are many examples reachable via Search engines of XML parsing in APEX – cropredy Dec 22 '14 at 18:05
1

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());
        }
    }
}
4
  • 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. Below is small part of my XML data. <OrderDetails> <OrderDetailID>2584</OrderDetailID> <GiftWrapCost>0.0000</GiftWrapCost> <GiftWrapNote/> <ProductCode>800006</ProductCode> </OrderDetails> – Nayana Pawar Dec 22 '14 at 11:37
  • Document example in the post explains quite clearly how to Get (Read) xml data from a source. This example is expecting a webservice response. If you are using a static resource/xml document then you might wanna do something like Dom.Document doc = new Dom.Document(); doc.load(xml); Read the xml file in to a string variable xml. – Mahmood Dec 22 '14 at 11:41
  • Yes know that how to get Root element and child element using XML parsing. But i want child of child. How to get this value. <Orders> <OrderID>3401</OrderID> <AddressValidated>Y</AddressValidated> <OrderDetails> <OrderDetailID>2584</OrderDetailID> <AutoDropShip>N</AutoDropShip> <FreeShippingItem>N</FreeShippingItem> <ProductID>37506</ProductID> <ProductName>Wet & Forget Outdoor 1 Gallon</ProductName> <Shipped>Y</Shipped> <TotalPrice>34.9900</TotalPrice> <Warehouses>1</Warehouses> </OrderDetails> </Orders> – Nayana Pawar Dec 22 '14 at 11:54
  • which value you want to get from the xml. you just entered the whole xml. – Mahmood Dec 22 '14 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.