0
 <?xml version="1.0" encoding="UTF-8"?>

I'll put just some extract of codes, that i think are meaningful.

I'm reading some information from one xml via http request, something like this :

        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

if i print the string xml to the screen i can see some problems with the codification already

then to return a document i have this

        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));

        doc = db.parse(is); 

Although i'm fetching correctly the information from http request, i'm having problems with the enconding of the characters when i'm showing the data.

I already tried to do is.setEncoding("UTF-8") but didn't work.

1 Answer 1

4

The problem is that you converted the xml to a String (characters), don't do that (you most likely used the wrong encoding and corrupted the xml). treat xml as binary data (bytes).

you could use EntityUtils.toByteArray (okay), or you could pass the HttpEntity stream directly to the xml parser (ideal).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply, is not possible that the problem is with the XML rather the code? And instead of doing EntityUtils.toString, i can use what you suggested, but then what should i put in the constructor of this object? is.setCharacterStream(new StringReader(xml)); //before when it was with a string is.setCharacterStream(???); // I don't know which constructor i can use with an array of bytes.
@unpix Use the InputSource(InputStream) constructor. If you start with a byte array, you can use ByteArrayInputStream.
Thanks, maybe it will work as well, but i already found the simplest solution
@unpix What did you find for solution ? I am also have this kind of problem.

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.