javaxmlstringjdom-2

Parsing response string to xml with jdom2


I want to parse my StringBuffer with Jdom2 after a SOAP call. But I didn't find a solution anywhere.

I saw a few solutions with w3.jdom, but they asked me to do with jdom2.

HttpResponse response = client.execute(httppost);

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();

String line = "";

while ((line = rd.readLine()) != null) {
    result.append(line);

}

Solution

  • Create a org.jdom2.input.SAXBuilder and use its build(Reader) method to create a JDOM2 Document object, where the Reader is created using new StringReader(result.toString()).

    However, copying all the data out of an HTTP response into a StringBuffer just so that you can then parse it is pretty inefficient. You're already getting an InputStream from the HttpResponse using response.getEntity().getContent(), and you can supply this InputStream directly to the SAXBuilder's build(InputStream) method.