0

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.

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

-1

You can't get the String back out of the StringReader. Either you assign the xmlRequest to a variable of its own or you have to create your own StringReader, which does that:

private static class OwnStringReader extends StringReader
{
    private final String content;

    public OwnStringReader(String content)
    {
        super(content);
        this.content=content;
    }

    public String getContent()
    {
        return content;
    }
}

Then you can retrieve your String by

    InputSource is=new InputSource(new OwnStringReader(xmlRequest));
    String xml=((OwnStringReader)is.getCharacterStream()).getContent();

2 Comments

'You can't get the String back out of the StringReader": of course you can. Just read all the characters, append then to a StringBuilder, and then get the string back from the builder.
@user207421 But it's a) not the same String and b) the Reader has to be reset afterwards (and probably before too).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.