javaxmlstringxml-parsingsaxparser

InputSource to String Conversion in JAVA


I have created InputSource object using my entire string xml request in my application and I am trying to fetch entire xml request from the created InputSource reference. Please find below code snippet and advise some code/ways to get the xmlRequest from the InputSource reference.

org.xml.sax.InputSource is = new org.xml.sax.InputSource(new StringReader(xmlRequest));

Now I wish to get xmlRequest from InputSource reference 'is'.

Can anybody please help me on this.


Solution

  • If you can live with recreating the request from the Reader, it's simple too:

        InputSource is=new InputSource(new StringReader(xmlRequest));
        Reader r=is.getCharacterStream();
        r.reset(); // Ensure to read the complete String
        StringBuilder b=new StringBuilder();
        int c;
        while((c=r.read())>-1)
            b.appendCodePoint(c);
        r.reset(); // Reset for possible further actions
        String xml=b.toString();