1

My XML response is

<name> DPD futár </name>

My XML parsing code

        SAXParserFactory _saxFactory = SAXParserFactory.newInstance();
        SAXParser _saxParser = _saxFactory.newSAXParser();
        XMLReader _xmlReader = _saxParser.getXMLReader();
        _shippingMethodList = new ArrayList<ShippingMethodItem>();
        ShippingMethodParser _cartLoginParser = new ShippingMethodParser(
                _shippingMethodList);
        _xmlReader.setContentHandler(_cartLoginParser);
        InputSource is = new InputSource();
        is.setEncoding("ISO-8859-1");
        is.setCharacterStream(new StringReader(response));
        _xmlReader.parse(is);

But I got following as string in my name variable.

DPD futár

I also try with

is.setEncoding("UTF-8");

But not getting success. Can anybody please help me regarding this ?

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113

2 Answers2

1

Try this one

InputSource in = new InputSource(new InputStreamReader(url.openStream(),"ISO-8859-1"));
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
0

You should not use ISO-8859-1 (latin 1) encoding for Hungarian data since it does not contain national characters like ő. The letter á is also a part of Portugese and Spanish alphabets for example and both are supported by latin 1, but in the case of Hungarian only latin 2 (ISO-8859-2) will work. However you should always use unicode encoding whenever possible.

For first I'd check if the received data is properly encoded since the error may also happen when you encode your string into unicode. The example you provide is a typical encoding error. Maybe you're trying to encode UTF-8 into UTF-8 which is wrong.

3k-
  • 2,467
  • 2
  • 23
  • 24
  • Not success after changing is.setEncoding("ISO-8859-2"); – Hardik Joshi Feb 27 '13 at 13:07
  • I think you receive your data in proper UTF-8 encoding but somehow forcing the parser to reencode it. You don't need that. Just process the XML as is. If SAX can't do it without touching the original encoding then try the Pull Parser, it works faster anyway. – 3k- Feb 27 '13 at 13:10