安卓:使用ISO-8859-1编码的SAXParser问题 [英] Android: SaxParser problems using ISO-8859-1 encoding
问题描述
林面临的XML与Android解析一些问题。问题是,从服务器的XML进来ISO-8859-1设置setEncoding(我得到<?XML版本=1.0编码=ISO-8859-1&GT ;。
)格式和Android设备看来,它忽略了编码
Im facing some problems on xml parsing 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.
例如,这是原始的XML来自服务器的一部分:
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>
这是我在手机上获得解析XML之前:
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>
正如你可以看到有这个词有问题的$ P $psentación的。
这是code其中我收到该文件,然后将其发送给解析器的一部分:
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);
这是我的解析器code:
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));
}
}
}
我不知道该怎么办。我试着设置recieving的XML字节ISO编码后,我创建的字符串,但我得到的唯一的事情就是一个的广场的而不是上的的
比你!
推荐答案
服务器可能的说的是ISO-8859-1,但它看起来像它的发送UTF-8。
The server may say it's ISO-8859-1 but it looks like it's sending UTF-8.
如果你的服务器code控制,确保你正确设置的输出流的编码。只需添加&LT;?XML版本=1.0编码=ISO-8859-1&GT;
头不引起输出是在正确的编码。
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.
这篇关于安卓:使用ISO-8859-1编码的SAXParser问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!