+ Start a Discussion
Dilip_VDilip_V 

XML file Parsing

Hi ,

I have little doubt about XML parsing.
How can we parse an XML file that contains 10 orders and how can we insert them.I tried with DOM class.Using dom I can extract the Information from xml but I am unable to store them into the list of orders with it's childs payments.

Below is my code.

Controller:
public class ParseTestv1 {

    // This holds the text we should parse
      public blob body{get;set;}

    // This holds the result of any parsing
    public String parsedText {get; set;}
    public list<string> ParsedList{get;set;}
     public String TextToParse {get; set;}
    
    // The main method that's called when you click the button
    public PageReference parse() {
        parsedlist=new list<string>();
       if (body == null) {
         parsedText = 'Nothing to parse';
       } else {
           TextToParse=body.toString();
         parsedText = parse(textToParse);
           
       }
        return null;
    }
    
    // Just checking that it's actually XML
    private String parse(String toParse) {
        
      DOM.Document doc = new DOM.Document();
      
      try {
        doc.load(toParse);    
        DOM.XMLNode root = doc.getRootElement();
        return walkThrough(root);
        
      } catch (System.XMLException e) {  // invalid XML
        return e.getMessage();
      }
    }

    // Recursively walk through the XML
    private 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) {
        result += + node.getName();
        if (node.getText().trim() != '') {
          result += '=' + node.getText().trim();
        }
        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 '';  //should never reach here
      
    }
}
Page:
<apex:page controller="ParseTestv1" sidebar="false" showHeader="false" >
   <apex:form >
       <apex:inputFile value="{!body}" />
     <apex:inputtextarea cols="40" rows="20" id="result" value="{!parsedText}"/>     
     <br/>
     <apex:commandButton value="Parse" action="{!parse}"/>
   </apex:form>
</apex:page>

Thanks.

 
Best Answer chosen by Dilip_V
pconpcon
If you take a look at the following code you will get a list of Order -> Header -> OrderOrigin
 
String url = 'http://paste.deadlypenguin.com/pcxsrklde/onzipm/raw';

Http h = new Http();

HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');


HttpResponse res = h.send(req);
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode orders = doc.getRootElement();

for (Dom.XMLNode order : orders.getChildren()) {
	Dom.XMLNode header = order.getChildElement('Header', null);
    
    if (header == null) {
        continue;
    }
    
    String orderOrigin = header.getChildElement('OrderOrigin', null).getText();
    System.debug(System.LoggingLevel.Error, orderOrigin);
}

All Answers

pconpcon
Can you please provide an example of the XML you are trying to parse.  There are some easier ways to get the data out using getChildElement [1].

[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_xml_dom.htm
Dilip_VDilip_V
Can you please share your email address.
pconpcon
If you take a look at the following code you will get a list of Order -> Header -> OrderOrigin
 
String url = 'http://paste.deadlypenguin.com/pcxsrklde/onzipm/raw';

Http h = new Http();

HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');


HttpResponse res = h.send(req);
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode orders = doc.getRootElement();

for (Dom.XMLNode order : orders.getChildren()) {
	Dom.XMLNode header = order.getChildElement('Header', null);
    
    if (header == null) {
        continue;
    }
    
    String orderOrigin = header.getChildElement('OrderOrigin', null).getText();
    System.debug(System.LoggingLevel.Error, orderOrigin);
}
This was selected as the best answer
Dilip_VDilip_V
thank you for responding .It helped me a lot.

is it possible to extract and process data of 32 mb xml file using apex(using above apex class).??
If not how to do it.

Thanks
pconpcon
You should be able to do it but you will be limited to the timeout that is allowed (default of 10s max of 120,000ms) and the overall allowed heapsize.
Dilip_VDilip_V
ya.we have so many limits to fire.
32 mb data is located in box or some other sites.So Can we use batch class for that?

Is there any chance to get out of the limits for processing that file?


 
pconpcon
The only way around it is to modify your endpoint so that it returns a smaller XML file and then you make multiple callouts to get the data.