Android: SaxParser problems using ISO-8859-1 encoding
Im facing some problems on xml parsi开发者_如何学编程ng with android. The problem is that the xml from the server comes in "ISO-8859-1" set with setEncoding (i get <?xml version="1.0" encoding="ISO-8859-1"?>
) format and the android device seems that its ignoring that 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 the 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 code where i recieve 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 dont know what to do. I tried setting the string i create after recieving the xml bytes to ISO encoding, but the only thing i got was a "square" instead of "ón".
Than you!
The server may say it's ISO-8859-1 but it looks like it's sending UTF-8.
If you have control of the server code, make sure you are setting the encoding on the output stream correctly. Just adding the <?xml version="1.0" encoding="ISO-8859-1"?>
header does not cause the output to be in the correct encoding.
精彩评论