2

This is the webservice response I am Getting in XML Format.

<?xml version="1.0"?>
<ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
  <ListOrdersResult>
    <Orders>
      <Order>
        <LatestShipDate>2016-05-04T11:44:15Z</LatestShipDate>
        <OrderType>StandardOrder</OrderType>
        <PurchaseDate>2016-05-04T04:59:42Z</PurchaseDate>
        <BuyerEmail>abc@marketplace.amazon.in</BuyerEmail>
        <AmazonOrderId>402-1183244-123344</AmazonOrderId>
        <LastUpdateDate>2016-05-04T11:45:13Z</LastUpdateDate>
        <NumberOfItemsShipped>1</NumberOfItemsShipped>
        <ShipServiceLevel>Standard</ShipServiceLevel>
        <OrderStatus>Shipped</OrderStatus>
        <SalesChannel>Amazon.in</SalesChannel>
        <NumberOfItemsUnshipped>0</NumberOfItemsUnshipped>
        <BuyerName>AAAAAA</BuyerName>
        <OrderTotal>
          <CurrencyCode>INR</CurrencyCode>
          <Amount>55555</Amount>
        </OrderTotal>
        <IsPremiumOrder>false</IsPremiumOrder>
        <EarliestShipDate>2016-05-04T11:44:15Z</EarliestShipDate>
        <MarketplaceId>qwerty</MarketplaceId>
        <FulfillmentChannel>AFN</FulfillmentChannel>
        <PaymentMethod>Other</PaymentMethod>
        <ShippingAddress>
          <StateOrRegion>abc</StateOrRegion>
          <City>aaaa</City>
          <Phone>1234567890</Phone>
          <CountryCode>IN</CountryCode>
          <PostalCode>1234</PostalCode>
          <Name>zxcvb</Name>
          <AddressLine1>poiu</AddressLine1>
          <AddressLine2>lkjh</AddressLine2>
        </ShippingAddress>
        <IsPrime>false</IsPrime>
        <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
        <SellerOrderId>25652-25689-25698</SellerOrderId>
      </Order>
      <Order>
        <LatestShipDate>2016-05-04T11:44:15Z</LatestShipDate>
        <OrderType>StandardOrder</OrderType>
        <PurchaseDate>2016-05-04T04:59:42Z</PurchaseDate>
        <BuyerEmail>abc@marketplace.amazon.in</BuyerEmail>
        <AmazonOrderId>402-1183244-123344</AmazonOrderId>
        <LastUpdateDate>2016-05-04T11:45:13Z</LastUpdateDate>
        <NumberOfItemsShipped>1</NumberOfItemsShipped>
        <ShipServiceLevel>Standard</ShipServiceLevel>
        <OrderStatus>Shipped</OrderStatus>
        <SalesChannel>Amazon.in</SalesChannel>
        <NumberOfItemsUnshipped>0</NumberOfItemsUnshipped>
        <BuyerName>AAAAAA</BuyerName>
        <OrderTotal>
          <CurrencyCode>INR</CurrencyCode>
          <Amount>55555</Amount>
        </OrderTotal>
        <IsPremiumOrder>false</IsPremiumOrder>
        <EarliestShipDate>2016-05-04T11:44:15Z</EarliestShipDate>
        <MarketplaceId>qwerty</MarketplaceId>
        <FulfillmentChannel>AFN</FulfillmentChannel>
        <PaymentMethod>Other</PaymentMethod>
        <ShippingAddress>
          <StateOrRegion>abc</StateOrRegion>
          <City>aaaa</City>
          <Phone>1234567890</Phone>
          <CountryCode>IN</CountryCode>
          <PostalCode>1234</PostalCode>
          <Name>zxcvb</Name>
          <AddressLine1>poiu</AddressLine1>
          <AddressLine2>lkjh</AddressLine2>
        </ShippingAddress>
        <IsPrime>false</IsPrime>
        <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
        <SellerOrderId>25652-25689-25698</SellerOrderId>
      </Order>
 </Orders>
    <CreatedBefore>2016-05-04T05:30:00Z</CreatedBefore>
  </ListOrdersResult>
  <ResponseMetadata>
    <RequestId>123564-25416-49df-2565-36524155</RequestId>
  </ResponseMetadata>
</ListOrdersResponse>

I want to create a Order Records but to Parse This XML

string output_result = res.getBody();

I've tried Option A

public class Parser_New{

    public class MonthlyReport {
        public String LatestShipDate {get; set;}
        public String OrderType {get; set;}
        public String PurchaseDate {get; set;}
    }

    public List<MonthlyReport> parse(string toParse) {
        List<MonthlyReport> reports = new List<MonthlyReport>();
        DOM.Document doc = new DOM.Document();
        doc.load(toParse);    
        parseReports(doc.getRootElement(), reports);

        system.debug('DDDDDDDDDDDDDDDDDDDDDDDD'+ Reports);
        return reports;
    }

    private void parseReports(DOM.XMLNode node, List<MonthlyReport> reports) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'Order') {
                MonthlyReport r = new MonthlyReport();
                parseReport(child, r);
                reports.add(r);
            }
        }
    }

    private void parseReport(DOM.XMLNode node, MonthlyReport r) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'LatestShipDate') {
                r.LatestShipDate= child.getText().trim();
            } else if (child.getName() == 'OrderType') {
                r.OrderType= child.getText().trim();
            } else if (child.getName() == 'PurchaseDate') {
                r.PurchaseDate= child.getText().trim();
            }
        }
    }
}

But I am not Getting any Result in the p.parse(output_result);

OPTION B

 Public String walkThrough(DOM.XMLNode node) {
        String result = '\n';
        if (node.getNodeType() == DOM.XMLNodeType.COMMENT) {
            return 'Comment (' +  node.getText() + ')';
        }
        if (node.getNodeType() == DOM.XMLNodeType.TEXT) {
            return 'Text (' + node.getText() + ')';
        }
        if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) {

            if (node.getText().trim() != '') {
                outxmlstring += node.getName()+'=' +node.getText().trim()+'\n';
                String nName = node.getName();

            }
            if (node.getAttributeCount() > 0) { 
                for (Integer i = 0; i< node.getAttributeCount(); i++ ) {
                    result += ', attribute #' + i + ':' + node.getAttributeKeyAt(i) + '=' + node.getAttributeValue(node.getAttributeKeyAt(i), node.getAttributeKeyNsAt(i));
                }  
            }
            for (Dom.XMLNode child: node.getChildElements()) {
                result += walkThrough(child);
            }
            return result;
        }
        return '';
    } 

Above method invoking like String Resultt = walkThrough(L1);

Where L1 is

Dom.XMLNode L1 = docx.getRootElement();

In both the Options I am Not result. So..Is it a correct way or there is a error in Code ??

1

Looks like you are close with 'Option A'.

The issue you're running into is that you're not getting deep enough into your XML hierarchy before trying to look for individual orders.

Given your input XML (I'm abbreviating it)

<ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
  <ListOrdersResult>
    <Orders>
      <Order>
        ...
      </Order>
      ...
    </Orders>
  </ListOrdersResult>
</ListOrdersResponse>

When you call doc.getRootElement(), you're being placed at <ListOrdersResponse> node in your XML tree.

When you pass that to your parse() method, you call getChildElements(), which gets you to the <ListOrdersResult> node.

As 'ListOrdersResult' != 'Order', and ListOrdersResult is the only (direct) child of the document root, you don't execute the code in the if block, and you exit the loop.

The minimal modification needed to get your code working would be to traverse two more levels down the XML tree sometime before you check if (child.getName() == 'Order')

You can do this either before you call, or inside of, parseReports(). My preference would be to do this prior to the call.

public List<MonthlyReport> parse(string toParse) {
    List<MonthlyReport> reports = new List<MonthlyReport>();
    DOM.Document doc = new DOM.Document();
    doc.load(toParse);

    // Puts us at the <ListOrdersResponse> node;
    Dom.XmlNode root = doc.getRootElement();

    // Puts us at the <ListOrdersResult> node
    List<Dom.XmlNode> listOrdersResult = root.getChildElements();

    // Puts us at the <Orders> node. If we call getChildElements() on one of these, 
    //  we will get all of the <Order> nodes.
    List<Dom.XmlNode> orders = listOrdersResult[0].getChildElements(); 

    parseReports(orders[0], reports);

    system.debug('DDDDDDDDDDDDDDDDDDDDDDDD'+ Reports);
    return reports;
}

private void parseReports(DOM.XMLNode node, List<MonthlyReport> reports) {
    for (Dom.XMLNode child : node.getChildElements()) {
        if (child.getName() == 'Order') {
            MonthlyReport r = new MonthlyReport();
            parseReport(child, r);
            reports.add(r);
        }
    }
}
| improve this answer | |
  • @ Derek .Worked like a charm.. – user40138 May 6 '16 at 10:22

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.