Android: SaxParser problems using ISO-8859-1 encoding

Im facing some problems while parsing xml using android. The problem is that the xml from the server comes in the format "ISO-8859-1" with the format setEncoding (i get <?xml version="1.0" encoding="ISO-8859-1"?> ), And the Android device looks like that it ignores this encoding.

For example, this is part of the original xml that comes from the server:

 <Result Filename="Pautas para la Presentación RUP Iteraciones de Construcción.ppt"> <Path>C:\Documents and Settings\zashael\My Documents\PFC\RUP\Pautas para la Presentación RUP Iteraciones de Construcción.ppt</Path> <Hostname>computer_1</Hostname> <IP>192.168.0.5:27960</IP> <ModDate>01-ene-1601 2:06:34</ModDate> <Size>33.280 bytes</Size> </Result> 

And this is what I get on the phone before parsing xml:

 </Result> <Result Filename="Pautas para la Presentaci�n RUP Fase Inicio.ppt"> <Path>C:\Documents and Settings\zashael\My Documents\PFC\RUP\Pautas para la Presentaci�n RUP Fase Inicio.ppt</Path> <Hostname>computer_1</Hostname> <IP>192.168.0.5:27960</IP> <ModDate>01-ene-1601 1:32:06</ModDate> <Size>26.624 bytes</Size> </Result> 

As you can see, there is a problem with the word "presentación".

This is the part of the code where I get the file and then send it to the parser:

 do { auxMessage = ois.readObject(); if (auxMessage instanceof ComConstants) { receivedMessage = (ComConstants) auxMessage; Log.d("Client", "Client has Search Results"); //Charset charset = Charset.forName("ISO-8859-1"); //CharsetDecoder decoder = charset.newDecoder(); //CharsetEncoder encoder = charset.newEncoder(); String test; test = new String( receivedMessage.fileContent, 0, receivedMessage.okBytes); if (finalMessage == null) { finalMessage = test; } else { finalMessage += test; } /*try { // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer // The new ByteBuffer is ready to be read. ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(finalMessage)); // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string. // The new ByteBuffer is ready to be read. CharBuffer cbuf = decoder.decode(bbuf); String s = cbuf.toString(); finalMessage = s; } catch (CharacterCodingException e) { } }*/ } else { Log.d("Client", "Unexpected message " + auxMessage.getClass().getName()); break; } } while (!receivedMessage.lastMessage); //test encoding //String s = finalMessage; //finalMessage = new String(s.getBytes("ISO-8859-1")); System.out.println("antes de parsear" + finalMessage); SaxParser sap = new SaxParser(finalMessage); 

And this is my parser code:

 package citic.android.remoteir; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Iterator; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class SaxParser extends DefaultHandler{ @SuppressWarnings("unchecked") ArrayList myResults; private String tempVal; private SearchResult tempResults; @SuppressWarnings("unchecked") public SaxParser(String xmlString){ myResults = new ArrayList(); parseDocument(xmlString); /* In order to test */ printData(); } @SuppressWarnings("unchecked") public ArrayList getResults(){ return myResults; } private void parseDocument(String xmlString) { try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://xml.org/sax/features/namespaces",false); spf.setFeature("http://xml.org/sax/features/namespace-prefixes",true); SAXParser sp = spf.newSAXParser(); XMLReader xmlReader = sp.getXMLReader(); xmlReader.setContentHandler(this); StringReader sr = new StringReader(xmlString); InputSource is = new InputSource(sr); is.setEncoding("ISO-8859-1"); xmlReader.parse(is); }catch(SAXException se) { se.printStackTrace(); }catch(ParserConfigurationException pce) { pce.printStackTrace(); }catch (IOException ie) { ie.printStackTrace(); } } @SuppressWarnings("unchecked") private void printData(){ System.out.println("No of Results '" + myResults.size() + "'."); Iterator it = myResults.iterator(); while(it.hasNext()) { System.out.println(((SearchResult) it.next()).toString()); //System.out.println(it.next().toString()); } } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { tempVal = ""; if(qName.equalsIgnoreCase("Result")) { tempResults = new SearchResult(); tempResults.setName(attributes.getValue("Filename")); } } public void characters(char[] ch, int start, int length) throws SAXException { tempVal = new String(ch,start,length); } @SuppressWarnings("unchecked") public void endElement(String uri, String localName, String qName) throws SAXException { if(qName.equalsIgnoreCase("Result")) { myResults.add(tempResults); }else if (qName.equalsIgnoreCase("Hostname")) { tempResults.setHostname(tempVal); }else if (qName.equalsIgnoreCase("IP")) { tempResults.setIpad(tempVal); }else if (qName.equalsIgnoreCase("Path")) { tempResults.setPath(tempVal); /*}else if (qName.equalsIgnoreCase("Author")) { tempResults.setHostname(tempVal); }else if (qName.equalsIgnoreCase("File")) { tempResults.setIpad(tempVal); */}else if (qName.equalsIgnoreCase("ModDate")) { tempResults.setModDate(tempVal); }else if (qName.equalsIgnoreCase("Size")) { tempResults.setSize((tempVal)); } } } 

I do not know what to do. I tried to tweak the line that I create after receiving the ISO encoded xml bytes, but the only thing I got was a "square" instead of "ón".

Than you!

+1
source share
1 answer

The server may say this is ISO-8859-1, but it looks like it is sending UTF-8.

If you are managing the server code, make sure that you have correctly configured the encoding in the output stream. Just adding the header <?xml version="1.0" encoding="ISO-8859-1"?> Does not lead to the correct encoding of the output.

+1
source

Source: https://habr.com/ru/post/1337674/


All Articles