You need to sign in to do that
Don't have an account?
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
All Answers
Thanks sfdcfox,
you're a life saver.
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;"/>
<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;
}
}