0

I'm parsing an incoming xml feed, in android to use it in app widget, the problem, is that the french characters are not encoded correctly in that xml like that :

Super Promo � l'incontournable Alhambra Thalasso 5* Hammamet : La nuit du 29/08 � seulement 107.185 DT au lieu de 126.100 DT  en LPD

and I'm parsing the file like this :

            InputSource isrc = new InputSource(this.feed.openStream());
            isrc.setEncoding("UTF-8");

            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(isrc.getByteStream());
            Element root = dom.getDocumentElement();

Is there a way to get rid of these strange characters ?

Thanks.

2 Answers 2

2

You hard coded set the encoding to "UTF-8" but what encoding is actually used by the sender?

In an XML you usually get meta information up front like <?xml version="1.0" encoding="utf-8"?>. You should use the encoding-value from the meta information for correct encoding.

Another issue in the code is, that you basically bypass the encoding with the line Document dom = builder.parse(isrc.getByteStream());. You should pass the InputSource instead:

Document dom = builder.parse(isrc);.

I actually use a Reader as in the follwing code, because then I use Java's encoding directly:

Document dom = builder.parse(
    new InputSource(
        new InputStreamReader(
                feed.openStream(),
                "[encoding goes here, usually UTF-8]")));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, finally found what's going wrong, infact the producer set xml as "utf-8", but when I parsed it as "ISO-8859-1" everything goes OK
1

Please see this

I suggest trying to use UTF-16 encoding once

1 Comment

Thanks I saw that, but the problem, is that's not me who produces the xml and I have no hand on it to make it right. I must find a solution that processes this file as it is.

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.