2010-05-07 25 views
1

我從維基百科以下.GPX數據:MalformedByteSequenceException試圖解析XML

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="byHand" version="1.1" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"> 
    <wpt lat="39.921055008" lon="3.054223107"> 
    <ele>12.863281</ele> 
    <time>2005-05-16T11:49:06Z</time> 
    <name>Cala Sant Vicenç - Mallorca</name> 
    <sym>City</sym> 
    </wpt> 
</gpx> 

當我打電話給我的分析方法,我得到一個異常(見下文)。調用看起來是這樣的:

Document tmpDoc = getParsedXML(currentGPX); 

我的分析方法看起來像這樣(標準解析代碼,平平淡淡....):

public static Document getParsedXML(String fileWithPath){ 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db; 
    Document doc = null; 
    try { 
     db = dbf.newDocumentBuilder(); 
     doc = db.parse(new File(fileWithPath)); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return doc; 
    } 

這個簡單的代碼引發以下異常:

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 3-byte UTF-8 sequence. 
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) 
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) 
at Zeugs.getParsedXML(Zeugs.java:38) 
at Zeugs.main(Zeugs.java:25) 

我想錯誤在於第一個文件的格式,但我不知道確切的位置。 你能給我一個提示嗎?

+2

是你的文件真的UTF-8編碼? – Dormilich 2010-05-07 07:42:26

+2

如果你用'c'替換'Vicenç'中的'ç',會發生什麼?仍然有這個問題? – Oded 2010-05-07 07:43:01

+2

+1 Dormlich,Oded。該文件可能尚未以UTF-8格式保存。 – ChrisBD 2010-05-07 08:36:03

回答

5

我建議你的文件沒有以UTF-8格式保存。

2

我在我的一個程序中有同樣的錯誤報告。但是錯誤只發生在Windows控制檯中運行jar的時候。在linux或eclipse中(右鍵單擊主類文件>作爲Java應用程序運行),錯誤未發生。

這是我猜想,因爲Windows(Cp ..)與Linux和eclipse中的UTF-8設置的默認編碼。要在運行的jar只需將-Dfile.encoding = UTF8參數添加到JVM

java -Dfile.encoding=UTF8 -jar myjar.jar 

一個原因程序依賴於這個參數可以使用輸入流時,編碼並沒有明確規定時,更改默認或閱讀器實現。