2009-06-16 100 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; 

}