views:

94

answers:

5

Hi I am trying to fetch this xml response

<?xml version="1.0" encoding="utf-8"?>
<desc c="¥99"/>

but on my Android each time I get Â¥99 ,after parsing the xml, instead of correct data(i.e.¥99).Is there any way to parse this Currency data correctly.Please correct me if I am missing something. EDIT:Here is the code that is used to get xml

docBuilder = docBuilderFactory.newDocumentBuilder ();
    InputSource is = new InputSource ();
    is.setEncoding ("UTF-8");
    Document doc = null;

        is.setCharacterStream (new StringReader (xmlStr));
        doc = docBuilder.parse (is);

    if (doc != null)
    {
            doc.getDocumentElement ().normalize ();
            NodeList detail = doc.getElementsByTagName ("desc");
            String c = detail.item (0).getAttributes ().getNamedItem ("c").getNodeValue ();

    }
A: 

The answer depends on how you parse the XML. For instance, one of the overloaded Xml#parse() methods accepts an Xml.Encoding argument that you can use to specify that the source is UTF-8.

Other parsing methods likely have an option to set the encoding, as well. Just check the docs.

Of course, it's entirely possible that the server doesn't generate correct UTF-8, even though it's declared as such in the XML file.

benvd
@benvd I have posted the code (see edited section above) .I also did tried is.setEncoding ("UTF-8"). but it too is of no avail.
100rabh
A: 

It would help if we had the code where you are reading the XML stream. I would assume that you need to ensure you're reading the stream in the correct encoding (as the stream reader may not take the XML declaration's encoding into account.

tKe
@tKe I have posted the code (see edited section above)
100rabh
A: 

That doesn't look like well-formed XML. There is no attribute name, only the c element. That seems reason enough not to get correct results...

Pontus Gagge
@Pontus Gagge updated xml above
100rabh
A: 

OK, with the example code, I'd guess it's the setCharacterStream() that fouls things up. Without having tested it myself, the docs say that encodings are used only by byte streams, so use setByteStream() instead, or, better use the byte stream constructor of InputSource.

Pontus Gagge
@Pontus Gagge Well I tried your approach but still I get the old result
100rabh
A: 

Hi Thanks for your support I recognized the problem ,the method in which I was getting response from server had below code

String strResponse = EntityUtils.toString (response.getEntity ());

where `response.getEntity ().getContentEncoding () returned null so I changed it to

String strResponse = EntityUtils.toString (response.getEntity (),"UTF-8");

where "UTF-8" is default charset.

Thanks StackOverFlow!

100rabh
Glad you found it. I would have said the issue is where you're reading in the string `xmlStr`.
tKe