2009-06-16 101 views
2

我已經下載了堆棧溢出站點的xml轉儲。在將轉儲轉儲到mysql數據庫時,我一直運行到以下錯誤:出現異常:字符引用「某些字符集如&#x10」是無效的XML字符。Sax無效的XML字符異常

我用UltraEdit(它是一個800兆文件)從文件中刪除一些字符,但是如果我刪除了一個無效字符集並運行了解析器,我會收到識別更多無效字符的錯誤。有關如何解決這個問題的任何建議?

乾杯所有,

Ĵ

回答

2

您正在使用哪種轉儲?第一個版本出現了問題(不僅是無效字符,而且還出現了<),但它們應該已在second dump中修復。

對於它的價值,我使用兩個正則表達式替換了原始無效字符。替換「&#x0 [12345678BCEF];」和「」,每個都帶有「?」 - 當然,它們都是正則表達式。

+0

我使用的第一fecking轉儲,今晚我會用第二個來解決它。謝謝你的幫助。 – slotishtype 2009-06-16 13:33:38

2

在XML中允許使用的字符集是here。正如你所看到的,#x10不是其中之一。如果這些出現在stackoverflow轉儲中,那麼它不符合XML。

或者,您正在使用錯誤的字符編碼讀取XML。

1

您應該將文件轉換爲UTF-8 我在Java開發,下面是我的轉換

公共字符串FileUTF8Cleaner(文件XMLFILE){

String out = xmlfile+".utf8"; 
    if (new File(out).exists()) 
     System.out.println("### File conversion process ### Deleting utf8 file"); 
     new File(out).delete(); 
     System.out.println("### File conversion process ### Deleting utf8 file [DONE!]"); 

    try { 
     System.out.println("### File conversion process ### Converting file"); 
     FileInputStream fis = new FileInputStream(xmlfile); 
     DataInputStream in = new DataInputStream(fis); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 

     FileOutputStream fos = new FileOutputStream(out); 

     while ((strLine = br.readLine()) != null) { 

      fos.write(strLine.replaceAll("\\p{Cc}", "").getBytes()); 
      fos.write("\n".getBytes()); 
     } 

     fos.close(); 
     fis.close(); 
     in.close(); 
     br.close(); 
     System.out.println("### File conversion process ### Converting file [DONE)]"); 

    } catch(Exception e) { 
     e.printStackTrace(); 
    } 

     System.out.println("### File conversion process ### Processing file : "+xmlfile.getAbsolutePath()+" [DONE!]"); 
     return out; 

}