我使用SAXParser来解析包含俄语字符的XML。 XML看起来像这样:
...
<exercise>
<ID>2</ID>
<training_types_id>1</training_types_id>
<Name>Тройки</Name>
<last_modified>0000-00-00 00:00:00</last_modified>
<del_check>0</del_check>
</exercise>
...
应用程序在此代码上崩溃(并在节点上联机#34;名称&#34;其中包含俄语符号):
String exerciseNameValue = exerciseNameElement.getFirstChild().getNodeValue();
服务器以这种方式编码数据:
if (is_null($DOMDocument)) {
$DOMDocument =new DOMDocument('1.0', 'UTF-8');
$DOMDocument->formatOutput = true;
$this->xml_encode($mixed, $DOMDocument, $DOMDocument);
return $DOMDocument->saveXML();
我以这种方式收到数据:
HttpEntity entity = res.getEntity();
websiteData = EntityUtils.toString(entity, "UTF-8");
以这种方式解析:
private static Document ParseXMLString(String xmlString) {
// fix encoding problem
xmlString = xmlString.replaceAll("[^\\x20-\\x7e]", "");
DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document doc = null;
try {
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlString));
//inStream.setEncoding("UTF8");
db = factory.newDocumentBuilder();
doc = db.parse(inStream);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return doc;
}
黎明错误:
03-21 06:12:09.227: W/System.err(12256): java.lang.reflect.InvocationTargetException
03-21 06:12:09.228: W/System.err(12256): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 06:12:09.228: W/System.err(12256): at java.lang.reflect.Method.invoke(Method.java:511)
03-21 06:12:09.229: W/System.err(12256): at com.ms.mastersportApp.parsers.AppParser.inParseData(AppParser.java:58)
03-21 06:12:09.229: W/System.err(12256): at com.ms.mastersportApp.synchronization.Synchronization.SynchronizeDown(Synchronization.java:165)
03-21 06:12:09.229: W/System.err(12256): at com.ms.mastersportApp.threads.SyncThread.doInBackground(SyncThread.java:131)
03-21 06:12:09.230: W/System.err(12256): at com.ms.mastersportApp.threads.SyncThread.doInBackground(SyncThread.java:1)
03-21 06:12:09.230: W/System.err(12256): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-21 06:12:09.230: W/System.err(12256): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-21 06:12:09.231: W/System.err(12256): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-21 06:12:09.231: W/System.err(12256): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-21 06:12:09.231: W/System.err(12256): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-21 06:12:09.232: W/System.err(12256): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-21 06:12:09.232: W/System.err(12256): at java.lang.Thread.run(Thread.java:856)
03-21 06:12:09.232: W/System.err(12256): Caused by: java.lang.NullPointerException
03-21 06:12:09.233: W/System.err(12256): at com.ms.mastersportApp.parsers.XmlParser.ParseXMLExercisesStr(XmlParser.java:179)
03-21 06:12:09.233: W/System.err(12256): at com.ms.mastersportApp.parsers.XmlParser.ParseXMLExercises(XmlParser.java:131)