+ Start a Discussion
YashavantYashavant 

System.XmlException: ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog.

HI,
I am calling one web service and getting response in XML and while iterating through that XML i got below exception

System.XmlException: ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog.

Code:
 HttpRequest req = new HttpRequest();
        String url='abaccc';

        req.setEndpoint(url);
        req.setMethod('GET');
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        XmlStreamReader reader = res.getXmlStreamReader();
        // Read through the XML
        while(reader.hasNext()) {
            System.debug('Event Type:' + reader.getEventType());
            if (reader.getEventType() == XmlTag.START_ELEMENT) {
            System.debug('Local name :'+reader.getLocalName());
            }
            reader.next();// Getting exception at this line.
        }

 Can anybody help me?

Thanks
Yash

goabhigogoabhigo

Hey Yash,did you get solution for this????

bradke32bradke32

Occasionally the XML stream that is returned has a "hidden" character before the first "<".  In this case you will receive the  ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog error message.  In order to move past this hidden character and get to the xml you are trying to parse use the following:

 

//Obviously do your setup before this point

//Send the request and return a response
HttpResponse res = h.send(req);

result = res.getBody();                                                //Get the result
result = result.substring(1);                                       //Move past the first character but keep the remainder of the string (which is the XML you want)

 

This worked for me.

Marvin BonifacioMarvin Bonifacio

Just encountered the same error, in my case, I used next() method initially in traversing the StreamReader else to move to the next node, before doing the loop --

 

XmlStreamReaderVariable.next();

 


sushil77sushil77
I am also facing same problem, @bradke32 Its not working
Rajasekhar Reddy 44Rajasekhar Reddy 44
@bradke32 that worked !!! tq
Lakshmi_lb14447Lakshmi_lb14447
Thanks @bradke32, that worked perfectly.