我正在嘗試使用Java和SAX爲Android設備解析XML文件。我從互聯網上獲得,並解析它時,我得到一個ExpatException:對字符「é」沒有格式良好(無效標記)。 有沒有辦法處理這些字符,而不必更改xml文件中的所有特殊字符?處理特殊字符的SAX
編輯: 這是我的代碼寫入我的SDcard的文件的一部分。
File SDCardRoot = Environment.getExternalStorageDirectory();
File f = new File(SDCardRoot,"edt.xml");
f.createNewFile();
FileOutputStream fileOutput = new FileOutputStream(f);
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
這裏是我的xml的一部分:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="ttss.xsl"?>
<timetable>
<option combined="0" totalweeks="0" showemptydays="0" dayclass="reverse">
<link href="g56065.xml" class="xml">Imprimer</link>
<link href="g56065.pdf" class="pdf">Version PDF</link>
<weeks>Semaines</weeks>
<dates>Dates</dates>
<week>Semaine</week>
<date>Date</date>
<all>Toutes les semaines</all>
<notes>Remarques</notes>
<id>ID</id>
<tag>Champs Libre</tag>
<footer>Publié le 10/09/2011 22:14:28</footer>
... </timetable>
這裏是解析代碼:
public class ParserSemaines extends DefaultHandler {
private final String SEMAINE = "span";
private final String DESCRIPTION = "description";
private ArrayList<Semaine> semaines;
private boolean inSemaine;
private Semaine currentSemaine;
private StringBuffer buffer;
@Override
public void processingInstruction(String target, String data) throws SAXException {
super.processingInstruction(target, data);
}
public ParserSemaines() {
super();
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
semaines = new ArrayList<Semaine>();
}
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
buffer = new StringBuffer();
if (localName.equalsIgnoreCase(SEMAINE)){
this.currentSemaine = new Semaine();
this.currentSemaine.setDate(attributes.getValue("date"));
this.inSemaine = true;
}
if(localName.equalsIgnoreCase(DESCRIPTION)){
this.currentSemaine.setDescription(buffer.toString());
}
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
if (localName.equalsIgnoreCase(SEMAINE)){
this.semaines.add(currentSemaine);
this.inSemaine = false;
}
}
public void characters(char[] ch,int start, int length) throws SAXException{
String lecture = new String(ch,start,length);
if(buffer != null) buffer.append(lecture);
}
public ArrayList<Semaine> getData(){
return semaines;
}
}
這裏是我用來調用解析器代碼:
SAXParserFactory fabrique = SAXParserFactory.newInstance();
SAXParser parseur = null;
ArrayList<Semaine> semaines = null;
try {
parseur = fabrique.newSAXParser();
DefaultHandler handler = new ParserSemaines();
File f = new File(Environment.getExternalStorageDirectory(),"edt.xml");
parseur.parse(f, handler);
semaines = ((ParserSemaines) handler).getData();
}
詢問是否需要其他代碼部分。
檢查後,SDcard中的xml文件顯示「é」爲「½」。 這應該是問題,但我沒有任何線索爲什麼。 我也嘗試用URI解析,但它不會改變任何我總是得到相同的異常。
SAX解析器應該處理非ASCII字符沒有任何問題。顯示你的代碼,以及你的XML的例子。 – parsifal
聲音如下:1. XML文件編碼錯誤,或2. XML文件在Internet上正確傳輸,其HTTP編碼指示的字符編碼,並在本地保存文件時丟失了該信息。 –
顯示的代碼只是將數據複製爲原始字節,因此不能以任何方式將XML編碼混淆。您需要顯示解析代碼。 –