Discard unprintable characters returned in server's XML response

Posted by Penang on Stack Overflow See other posts from Stack Overflow or by Penang
Published on 2011-01-09T08:14:40Z Indexed on 2011/01/09 12:53 UTC
Read the original article Hit count: 137

Filed under:
|
|
|

While trying to use the Bing API to search, I am getting characters that are not printable and do not seem to hold any extra information. The goal is to save the XML (UTF-8) response as a text file to be parsed later.

My code currently looks something like this:

    URL url = new URL(queryURL);

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter out = new BufferedWriter(new FileWriter(query+"-"+saveResultAs));
    String str = in.readLine();
    out.write(str);

    in.close();
    out.close();

When I send the contents of 'str' to console it looks something like this:

alt text

and here's a what the newly created local XML file looks like:

alt text

What should I be doing to convert the UTF-8 text so that str does not have the extra characters?

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml