function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kdaviskdavis 

XML parsing infinite loop

Hi, 

I'm trying to parse some XML with the following code

 

public class AnalystReportsParser implements XMLParser{

public virtual RSINews__c[] parseXml(XmlStreamReader xml) {
RSINews__c[] news = new RSINews__c[0];
while(xml.hasNext()) {

  if (xml.getEventType() == XmlTag.START_ELEMENT) {

   if ('Page' == xml.getLocalName()){

       if('en-us' == xml.getAttributeValue(null, 'Locale')){
             RSINews__c record = new RSINews__c ();

 

                  ''

                  ''

             news.add(record);
        }
     }

      xml.next();
    }
}
return news;
}
}

 

However I keep recieving the error:

System.LimitException: Too many code statements: 200001

And it indicates that it occurs in my if loop:

 

if (xml.getEventType() == XmlTag.START_ELEMENT) {

 

any ideas why this occurs?

thx

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You're not advancing to the next element because you only advance on a start element. Move the xml.next() call outside the if statement.

All Answers

sfdcfoxsfdcfox
You're not advancing to the next element because you only advance on a start element. Move the xml.next() call outside the if statement.
This was selected as the best answer
kdaviskdavis

Thanks sfdcfox,

 

you're a life saver.

Ramakrishnan AyyanarRamakrishnan Ayyanar
I created one simple XML Parsing.I have Used one VF page and Apex Class.It's parsing correctly ...

VF Page:

<apex:page Controller="Xmlparsar">

<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}" />  
                <apex:commandButton value="ParseXML File" action="{!Parsexmlfile}"/>
            </apex:pageBlockButtons>
            <apex:inputTextArea value="{!xmlstring}" style="width:336px;height:260px;"/> &nbsp;&nbsp;
            <apex:inputTextArea value="{!outxmlstring}" style="width:336px;height:260px;" id="response"/><br/>

            <apex:outputLabel value="Select Xml File" for="file"/>
            <apex:inputFile fileName="{!fileName}" value="{!body}"/>
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

Apex Class:

public  class Xmlparsar
{
    //xml string
    public String xmlstring{get;set;}
  
    //display xml string
    public String outxmlstring{get;set;}
  
    //rootelement
    public String rootElement{get;set;}
  
    //
    public String filename{get;set;}
  
    public blob body{get;set;}
     
    //constructor
    public Xmlparsar()
    {
   
    }
  
   
//Parsing xml what you entered in the left text area
    public pagereference Parsexml()
    {
       DOM.Document xmlDOC = new DOM.Document();
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       //.getChildren())
       {       
        
        
          loadChilds(xmlnodeobj);        
       }     
       return null;
    }
  
    //loading the child elements
    private void loadChilds(DOM.XMLNode xmlnode)
    {
        for(Dom.XMLNode child : xmlnode.getChildElements())
        {
          if(child.getText()!= null)
          {
          outxmlstring+='\n'+child.getName()+': '+child.getText();
      
          }
          loadChilds(child);      
        }
    }
  
  
//This is for parsing xml file what you selected
  public pagereference Parsexmlfile()
  {
       DOM.Document xmlDOC = new DOM.Document();
       xmlstring=body.tostring();        
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());//gives you root element Name
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       {       
                 
          loadChilds(xmlnodeobj);
       }     
      return null;
    }
}