-3

I want to insert XML Response (Quantity,InStock) into salesforce Custom object(Test__c) in custom fields (Quantity__c and Instock__c) based on SellerSKU.see below XML Response. Please Help me how to do xml parsing into apex.

<SupplyResult>
 <MarketId>xxxxxxxx</MarketId>
 <SupplyList>
    <member>
        <Quantity>9</Quantity>
        <InStock>5</InStock>
        <SellerSKU>Test- 1111</SellerSKU>
    </member>
  </SupplyList>
  </SupplyResult>
3
  • 3
    What have you tried? Have you done any research? Have you hit any specific roadblocks? Right now this question reads like you haven't bothered to search. See: Reading and Writing XML Using the DOM
    – Adrian Larson
    Sep 22 '16 at 12:10
  • Thanks Adrian , i did integration salsforce to AmazonMWS, i got above response,but i want save that elements values in salesforce,i tried but am strucked in xml parsing. Sep 22 '16 at 13:23
  • 3
    A quick search on the xml tag, sorted by votes, provides good learning material, such as Difficulty Parsing XML Sep 22 '16 at 13:48
1

I think you will find everything you need to accomplish the task at hand in the Apex Developer Guide documentation: Reading and Writing XML Using the DOM. You can use the Dom.Document class to do the heavy lifting. This would also be a good time to define some inner classes to do your data processing. Something like:

public class SupplyResult
{
    final String marketId;
    final List<SupplyMember> members;
    public SupplyResult(String xml)
    {
        Dom.Document doc = new Dom.Document();
        doc.load(xml);

        //further processing
    }
}
public class SupplyMember
{
    final Integer quantity, inStock;
    final String sellerSKU;
    public Supply(Dom.XmlNode node)
    {
        quantity = Integer.valueOf(node.getChildElement('Quantity', null).getText());
        // etc
    }
}

It will be a better learning experience if you fill in the gaps above instead of having me provide every step. It should be straightforward. I only omitted eight lines of code (though you could do it with fewer if you were so inclined).

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