gpt4 book ai didi

xml - Android:使用 ISO-8859-1 编码的 SaxParser 问题

转载 作者:行者123 更新时间:2023-11-30 04:52:20 26 4
gpt4 key购买 nike

我在用 android 解析 xml 时遇到了一些问题。问题是来自服务器的 xml 以设置了 setEncoding(我得到 <?xml version="1.0" encoding="ISO-8859-1"?>)格式的“ISO-8859-1”格式出现,而 android 设备似乎忽略了该编码。

例如,这是来自服务器的原始 xml 的一部分:

<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 之前通过电话获得的信息:

</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>

如您所见,“presentación” 这个词有问题。

这是我接收文件的代码部分,然后将其发送给解析器:

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);

这是我的解析器代码:

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));
}
}


}

我不知道该怎么办。我尝试将收到 xml 字节后创建的字符串设置为 ISO 编码,但我得到的唯一结果是 "square" 而不是 "ón"

比你还好!

最佳答案

服务器可能它是 ISO-8859-1,但看起来它发送的是 UTF-8。

如果您可以控制服务器代码,请确保您正确设置了输出流的编码。只需添加 <?xml version="1.0" encoding="ISO-8859-1"?> header 不会导致输出采用正确的编码。

关于xml - Android:使用 ISO-8859-1 编码的 SaxParser 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com