Skip to Content
avatar image
Former Member

Error while parsing a StringReader: Root element is required

Hi

I have a strange issue of with StringReader stream is getting closed. please check the code below

public static GenericDataInstance readGdi(StringReader aReader) {

...

SAXParserFactory lParserFactory = PartialCopyOfXMLServices.getParserFactory();

SAXParser lParser = lParserFactory.newSAXParser();

XMLReader lReader = lParser.getXMLReader(); *<======= "aReader" Stream is getting closed*

// set content handler

....

In the above code whenever the lParser.getXmlReader is called then,

"aReader" object's stream is getting closed.

has any one faced a similar kind of issue?

regards

Rajesh R

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • avatar image
    Former Member
    Feb 10, 2009 at 10:48 AM

    Hi Rajesh,

    Where are you pasing the StringReader, i don't see any where you are passing the reference of StringReader. if you missed that can you provide detailed code snippet.

    Regards.

    krishna.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi

      if you see the parameter being passed to the method

      public static GenericDataInstance readGdi(StringReader aReader) {

      'aReader is a String reader.

      The strange part is, the 'aReader' is not passed to the SAXparser, but still the reader is getting closed.

      regards

      Rajesh R

  • avatar image
    Former Member
    Feb 11, 2009 at 05:46 AM

    Hi,

    Yes, here we are not using the aReader any where in the method, The SAXParser's Parse method accpets InputStream. For this case, i have tried something of this kind, check the bellow code.

    public void parseXMLDoc(StringReader aReader) {

    DefaultHandler handler = this;

    SAXParserFactory factory = SAXParserFactory.newInstance();

    }

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 11, 2009 at 05:52 AM

    Hi,

    public void parseXML(StringReader aReader) {

    DefaultHandler handler = this;

    SAXParserFactory factory = SAXParserFactory.newInstance();

    SAXParser saxParser = factory.newSAXParser();

    XMLReader iread=saxParser.getXMLReader();

    }

    When i execute this, normally it does nothing, so my output is nothing, i didn't get any errors like aReader is close like that, it works ok for me.

    When calling the method, there i initialized aReader to a String like below.

    StringReader aReader_call=new StringReader(" XMLString");

    try anywhere you missed initializing aReader or closing the object before the call..

    Regards..

    Krishna..

    Add comment
    10|10000 characters needed characters exceeded