2016-03-11 33 views
0

這是繼續of another question。當我嘗試解析我的xml文件時,出現此錯誤。「內容在尾部不允許。」用SAX解析java時

Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 68; columnNumber: 12; Content is not allowed in trailing section. 
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) 
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.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.AbstractSAXParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source) 
at javax.xml.parsers.SAXParser.parse(Unknown Source) 
at convert.ExcelXmlReader.getAndParseFile(ExcelXmlReader.java:55) 
at convert.ExcelXmlReader.main(ExcelXmlReader.java:24) 

「lineNumber:68; columnNumber:12;」部分與我的xml文件中最後一個'>'匹配。當我嘗試刪除它後面的空白空間時,它仍然給我錯誤。我試圖把它扔進xml validator,但它沒有提出任何東西。我只是不確定我在做什麼。我嘗試了其他堆棧溢出問題的其他解決方案(通過查看我的文件以查找xml文件後的任何奇怪字符,確保所有標記都已關閉),但沒有一個適用於我。

有沒有人有我現在應該去的提示?哪個會是最好的方向?

<?xml version="1.0" encoding="utf-16"?> 
<?mso-application progid="Excel.Sheet"?> 

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"> 
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> 
<Author>marc</Author> 
<LastAuthor>ESDI</LastAuthor> 
</DocumentProperties> 
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> 
<WindowHeight>7560</WindowHeight> 
<WindowWidth>12300</WindowWidth> 
<WindowTopX>360</WindowTopX> 
<WindowTopY>135</WindowTopY> 
<ProtectStructure>False</ProtectStructure> 
<ProtectWindows>False</ProtectWindows> 
</ExcelWorkbook> 
<Styles> 
<Style ss:ID="Default" ss:Name="Normal"> 
<Alignment ss:Vertical="Bottom"/> 
<Borders/> 
<Font/> 
<Interior/> 
<NumberFormat/> 
<Protection/> 
</Style> 
<Style ss:ID="s21"> 
<NumberFormat ss:Format="Short Date"/> 
</Style> 
</Styles> 
<Worksheet ss:Name="Sheet1"> 
<Table x:FullColumns="1" x:FullRows="1"> 
      <Row> 
    <Cell><Data ss:Type="String">Crt. Dte</Data></Cell> 
    <Cell><Data ss:Type="String">WR Status</Data></Cell> 
    <Cell><Data ss:Type="String">Request Plant</Data></Cell> 
    <Cell><Data ss:Type="String">Request #</Data></Cell>  
    <Cell><Data ss:Type="String">Item#</Data></Cell> 
    <Cell><Data ss:Type="String">Request Cost Center</Data></Cell> 
    <Cell><Data ss:Type="String">WR Description</Data></Cell> 
    <Cell><Data ss:Type="String">W/O No</Data></Cell> 
    <Cell><Data ss:Type="String">Charge Plant</Data></Cell> 
    <Cell><Data ss:Type="String">Charge Cost Center</Data></Cell> 
    <Cell><Data ss:Type="String">Equip NO</Data></Cell> 
    <Cell><Data ss:Type="String">Equipment Name</Data></Cell> 
    <Cell><Data ss:Type="String">Required Date</Data></Cell> 
    <Cell><Data ss:Type="String">WO Type</Data></Cell> 
    <Cell><Data ss:Type="String">Exec. C/C</Data></Cell> 
    <Cell><Data ss:Type="String">Exec. Plant</Data></Cell> 
    <Cell><Data ss:Type="String">Plant1</Data></Cell> 
    <Cell><Data ss:Type="String">Area</Data></Cell> 
    <Cell><Data ss:Type="String">Confirmed</Data></Cell> 
    <Cell><Data ss:Type="String">WO Status</Data></Cell> 
    <Cell><Data ss:Type="String">W/R Requester</Data></Cell> 

      </Row> 

</Table> 
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
<Selected/> 
<ProtectObjects>False</ProtectObjects> 
<ProtectScenarios>False</ProtectScenarios> 
</WorksheetOptions> 
</Worksheet> 
</Workbook> 

解析的當前代碼。大多數其他代碼在上面鏈接的上一個問題中。

private static void getAndParseFile() throws Exception { 
     System.out.println("getAndParseFile"); 
     String fileName="C:\\Users\\windowsUserName\\Downloads\\F7BAH1P_List.xml"; 

     File file = new File(fileName); 

     removeLineFromFile(file.getAbsolutePath()); 
     System.out.println("Finished Removing Lines"); 


     String fileContent = IOUtils.toString(new FileInputStream(file)); 
     fileContent = fileContent.substring(0, fileContent.lastIndexOf('>')+1); 
     fileContent = fileContent.replaceAll("&#",""); 



     PrintWriter pw = null; 
     pw = new PrintWriter(new FileWriter("C:\\Users\\windowsUserName\\Downloads\\tempfile.txt")); 
     pw.println(fileContent); 
     pw.flush(); 

     ByteArrayInputStream bis = new ByteArrayInputStream(Charset.forName("UTF-16").encode(fileContent).array()); 


     SAXParserFactory parserFactor = SAXParserFactory.newInstance(); 
     SAXParser parser = parserFactor.newSAXParser(); 
     SAXHandler handler = new SAXHandler(); 

     parser.parse(bis, handler); 

    } 

的RemoveLineFromFile刪除從開始和從XML文件是空白的或包含一些計數器/標題數據的末尾2 <row></row>

private static void removeLineFromFile(String file) { 

     BufferedReader br = null; 
     PrintWriter pw = null; 
     try { 
      File inFile = new File(file); 
      if (!inFile.isFile()) { 
       return; 
      } 

      br = new BufferedReader(new FileReader(file)); 

      String line = null; 
      int totalRows=0; 
      boolean continueMethod = false; 
      //Count total number of rows in file 
      while ((line = br.readLine()) != null) { 
       //check if file is already formatted 
       if (line.contains("List for Work")){ 
        continueMethod = true; 
       } 

       if (line.toLowerCase().contains("</row>")){ 
         ++totalRows; 
        } 
       } 

      if (continueMethod) 
      { 
       //Create a temporary file to hold the file with deleted lines. 
       File tempFile = new File(inFile.getAbsolutePath() + ".tmp"); 
       pw = new PrintWriter(new FileWriter(tempFile)); 

       line = null; 
       br.close(); 
       br = null; 
       br = new BufferedReader(new FileReader(file)); 
       boolean ignoreMe = false; 
       int rowCounter = 0; 
       int rowCloser = 0; 
       //begin cycling through file and writing to new one. 
       while((line = br.readLine()) != null) 
       { 
        //if runs into a row, count it. 
        if (line.toLowerCase().contains("<row>")){ 
         rowCounter++; 
        } 
        if (line.toLowerCase().contains("</row>")){ 
         rowCloser++; 
        } 
        //Delete the first two, and last two lines 
        if ((rowCounter == 1) || (rowCounter == 2) || (rowCounter == (totalRows-1)) || (rowCounter == totalRows)) 
        { 
         ignoreMe = true; 
         //If it reached the last closing tag, exit out of this to allow it to write the rest of the file. 
         if (rowCloser==totalRows) 
          rowCounter++;     
        } 
        else 
        { 
         ignoreMe = false; 
        } 
        //copy over other lines 
        if (!ignoreMe) 
        { 
         pw.println(line); 
         pw.flush(); 
        } 
       } 
       br.close(); 
       pw.close(); 
       //Delete the original file 
       if (!inFile.delete()) { 
        System.out.println("Could not delete original file"); 
        return; 
       } 

       //Rename the new file to the filename the original file had. 
       if (!tempFile.renameTo(inFile)) 
        System.out.println("Could not rename temp file"); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

下面是XML文件之前通過 「removelinefromfile」 走出

<?xml version="1.0" encoding="utf-16"?> 
<?mso-application progid="Excel.Sheet"?> 

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"> 
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> 
<Author>marc</Author> 
<LastAuthor>ESDI</LastAuthor> 
</DocumentProperties> 
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> 
<WindowHeight>7560</WindowHeight> 
<WindowWidth>12300</WindowWidth> 
<WindowTopX>360</WindowTopX> 
<WindowTopY>135</WindowTopY> 
<ProtectStructure>False</ProtectStructure> 
<ProtectWindows>False</ProtectWindows> 
</ExcelWorkbook> 
<Styles> 
<Style ss:ID="Default" ss:Name="Normal"> 
<Alignment ss:Vertical="Bottom"/> 
<Borders/> 
<Font/> 
<Interior/> 
<NumberFormat/> 
<Protection/> 
</Style> 
<Style ss:ID="s21"> 
<NumberFormat ss:Format="Short Date"/> 
</Style> 
</Styles> 
<Worksheet ss:Name="Sheet1"> 
<Table x:FullColumns="1" x:FullRows="1"> 
<Row> 
<Cell><Data ss:Type="String">List for Work Request(F7BAH1P)</Data></Cell> 
</Row> 
<Row> 
</Row> 
      <Row> 
    <Cell><Data ss:Type="String">Crt. Dte</Data></Cell> 
    <Cell><Data ss:Type="String">WR Status</Data></Cell> 
    <Cell><Data ss:Type="String">Request Plant</Data></Cell> 
    <Cell><Data ss:Type="String">Request #</Data></Cell>  
    <Cell><Data ss:Type="String">Item#</Data></Cell> 
    <Cell><Data ss:Type="String">Request Cost Center</Data></Cell> 
    <Cell><Data ss:Type="String">WR Description</Data></Cell> 
    <Cell><Data ss:Type="String">W/O No</Data></Cell> 
    <Cell><Data ss:Type="String">Charge Plant</Data></Cell> 
    <Cell><Data ss:Type="String">Charge Cost Center</Data></Cell> 
    <Cell><Data ss:Type="String">Equip NO</Data></Cell> 
    <Cell><Data ss:Type="String">Equipment Name</Data></Cell> 
    <Cell><Data ss:Type="String">Required Date</Data></Cell> 
    <Cell><Data ss:Type="String">WO Type</Data></Cell> 
    <Cell><Data ss:Type="String">Exec. C/C</Data></Cell> 
    <Cell><Data ss:Type="String">Exec. Plant</Data></Cell> 
    <Cell><Data ss:Type="String">Plant1</Data></Cell> 
    <Cell><Data ss:Type="String">Area</Data></Cell> 
    <Cell><Data ss:Type="String">Confirmed</Data></Cell> 
    <Cell><Data ss:Type="String">WO Status</Data></Cell> 
    <Cell><Data ss:Type="String">W/R Requester</Data></Cell> 

      </Row> 






<Row> 
</Row> 
<Row> 
<Cell><Data ss:Type="String">Count: 244</Data></Cell> 
</Row> 
</Table> 
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
<Selected/> 
<ProtectObjects>False</ProtectObjects> 
<ProtectScenarios>False</ProtectScenarios> 
</WorksheetOptions> 
</Worksheet> 
</Workbook> 
+0

沒有看到'removeLineFromFile'的代碼,這將不可能回答你的問題。該錯誤表示在結束''標籤後有內容。 –

+0

@吉姆加里森,我編輯並添加上面。但我認爲它沒有做錯什麼?它只刪除文件中空白的兩個。 – arsarc

+0

再次編碼問題,就像原始問題一樣。 [FileWriter](https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html)和[FileReader](https://docs.oracle.com/javase/7/docs /api/java/io/FileReader.html)假定默認的字符編碼是可以接受的。您不能依靠它們以便攜方式處理UTF-16編碼文件。 – predi

回答

3

您可能會解析錯誤,是你的文件的編碼不匹配XML聲明中的編碼:

<?xml version="1.0" encoding="utf-16"?> 

FileWriterFileReader假定默認字符編碼是可接受的(我的系統上是UTF-8)。您不能依靠它們以便攜方式處理UTF-16編碼文件。以下是他們的文檔:

寫字符文件的便捷類。這個類的構造函數假定默認的字符編碼和默認的字節緩衝區大小是可以接受的。要自己指定這些值,請在FileOutputStream上構造一個OutputStreamWriter。

閱讀字符文件的便捷類。該類的構造函數假定默認的字符編碼和默認的字節緩衝區大小是合適的。要自己指定這些值,請在FileInputStream上構建一個InputStreamReader。

所以你需要做文檔建議 - 使用替代品。

這裏是與你的removeLineFromFile方法的三個不同的實現表明您的問題一些快速的測試代碼:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

public class Encoding { 

    private static File removeLineFromFile2(String file) { 

     File ret = null; 

     BufferedReader br = null; 
     PrintWriter pw = null; 
     try { 
      File inFile = new File(file); 
      if (!inFile.isFile()) { 
       return ret; 
      } 

      ret = inFile; 

      br = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-16")); 

      String line = null; 
      int totalRows=0; 
      boolean continueMethod = false; 
      //Count total number of rows in file 
      while ((line = br.readLine()) != null) { 
       //check if file is already formatted 
       if (line.contains("List for Work")){ 
        continueMethod = true; 
       } 

       if (line.toLowerCase().contains("</row>")){ 
         ++totalRows; 
        } 
       } 

      if (continueMethod) 
      { 
       //Create a temporary file to hold the file with deleted lines. 
       File tempFile = new File(inFile.getAbsolutePath() + ".2.tmp"); 
       pw = new PrintWriter(new OutputStreamWriter(
        new FileOutputStream(tempFile), "UTF-16")); 

       line = null; 
       br.close(); 
       br = null; 
       br = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-16")); 
       boolean ignoreMe = false; 
       int rowCounter = 0; 
       int rowCloser = 0; 
       //begin cycling through file and writing to new one. 
       while((line = br.readLine()) != null) 
       { 
        //if runs into a row, count it. 
        if (line.toLowerCase().contains("<row>")){ 
         rowCounter++; 
        } 
        if (line.toLowerCase().contains("</row>")){ 
         rowCloser++; 
        } 
        //Delete the first two, and last two lines 
        if ((rowCounter == 1) || (rowCounter == 2) || (rowCounter == (totalRows-1)) || (rowCounter == totalRows)) 
        { 
         ignoreMe = true; 
         //If it reached the last closing tag, exit out of this to allow it to write the rest of the file. 
         if (rowCloser==totalRows) 
          rowCounter++;     
        } 
        else 
        { 
         ignoreMe = false; 
        } 
        //copy over other lines 
        if (!ignoreMe) 
        { 
         pw.println(line); 
         pw.flush(); 
        } 
       } 
       br.close(); 
       pw.close(); 
       System.out.println("Temp file is: " + tempFile.getAbsolutePath()); 
       ret = tempFile; 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return ret; 
    } 

    private static File removeLineFromFile1(String file) { 

     File ret = null; 

     BufferedReader br = null; 
     PrintWriter pw = null; 
     try { 
      File inFile = new File(file); 
      if (!inFile.isFile()) { 
       return ret; 
      } 

      ret = inFile; 

      br = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-16")); 

      String line = null; 
      int totalRows=0; 
      boolean continueMethod = false; 
      //Count total number of rows in file 
      while ((line = br.readLine()) != null) { 
       //check if file is already formatted 
       if (line.contains("List for Work")){ 
        continueMethod = true; 
       } 

       if (line.toLowerCase().contains("</row>")){ 
         ++totalRows; 
        } 
       } 

      if (continueMethod) 
      { 
       //Create a temporary file to hold the file with deleted lines. 
       File tempFile = new File(inFile.getAbsolutePath() + ".1.tmp"); 
       pw = new PrintWriter(new FileWriter(tempFile)); 

       line = null; 
       br.close(); 
       br = null; 
       br = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-16")); 
       boolean ignoreMe = false; 
       int rowCounter = 0; 
       int rowCloser = 0; 
       //begin cycling through file and writing to new one. 
       while((line = br.readLine()) != null) 
       { 
        //if runs into a row, count it. 
        if (line.toLowerCase().contains("<row>")){ 
         rowCounter++; 
        } 
        if (line.toLowerCase().contains("</row>")){ 
         rowCloser++; 
        } 
        //Delete the first two, and last two lines 
        if ((rowCounter == 1) || (rowCounter == 2) || (rowCounter == (totalRows-1)) || (rowCounter == totalRows)) 
        { 
         ignoreMe = true; 
         //If it reached the last closing tag, exit out of this to allow it to write the rest of the file. 
         if (rowCloser==totalRows) 
          rowCounter++;     
        } 
        else 
        { 
         ignoreMe = false; 
        } 
        //copy over other lines 
        if (!ignoreMe) 
        { 
         pw.println(line); 
         pw.flush(); 
        } 
       } 
       br.close(); 
       pw.close(); 
       System.out.println("Temp file is: " + tempFile.getAbsolutePath()); 
       ret = tempFile; 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return ret; 
    } 

    private static File removeLineFromFile(String file) { 

     File ret = null; 

     BufferedReader br = null; 
     PrintWriter pw = null; 
     try { 
      File inFile = new File(file); 
      if (!inFile.isFile()) { 
       return ret; 
      } 

      ret = inFile; 

      br = new BufferedReader(new FileReader(file)); 

      String line = null; 
      int totalRows=0; 
      boolean continueMethod = false; 
      //Count total number of rows in file 
      while ((line = br.readLine()) != null) { 
       //check if file is already formatted 
       if (line.contains("List for Work")){ 
        continueMethod = true; 
       } 

       if (line.toLowerCase().contains("</row>")){ 
         ++totalRows; 
        } 
       } 

      if (continueMethod) 
      { 
       //Create a temporary file to hold the file with deleted lines. 
       File tempFile = new File(inFile.getAbsolutePath() + ".tmp"); 
       pw = new PrintWriter(new FileWriter(tempFile)); 

       line = null; 
       br.close(); 
       br = null; 
       br = new BufferedReader(new FileReader(file)); 
       boolean ignoreMe = false; 
       int rowCounter = 0; 
       int rowCloser = 0; 
       //begin cycling through file and writing to new one. 
       while((line = br.readLine()) != null) 
       { 
        //if runs into a row, count it. 
        if (line.toLowerCase().contains("<row>")){ 
         rowCounter++; 
        } 
        if (line.toLowerCase().contains("</row>")){ 
         rowCloser++; 
        } 
        //Delete the first two, and last two lines 
        if ((rowCounter == 1) || (rowCounter == 2) || (rowCounter == (totalRows-1)) || (rowCounter == totalRows)) 
        { 
         ignoreMe = true; 
         //If it reached the last closing tag, exit out of this to allow it to write the rest of the file. 
         if (rowCloser==totalRows) 
          rowCounter++;     
        } 
        else 
        { 
         ignoreMe = false; 
        } 
        //copy over other lines 
        if (!ignoreMe) 
        { 
         pw.println(line); 
         pw.flush(); 
        } 
       } 
       br.close(); 
       pw.close(); 
       System.out.println("Temp file is: " + tempFile.getAbsolutePath()); 
       ret = tempFile; 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return ret; 
    } 

    private static void parse(File file) { 
     try { 
      System.out.println("Parsing " + file.getAbsolutePath()); 

      SAXParserFactory parserFactor = SAXParserFactory.newInstance(); 
      SAXParser parser = parserFactor.newSAXParser(); 
      DefaultHandler handler = new DefaultHandler(); 

      parser.parse(file, handler); 
     } catch (Exception ex) { 
      System.out.println("An exception occurred: " + ex.getMessage()); 
     } finally { 
      System.out.println("Done with " + file.getAbsolutePath()); 
     } 
    } 

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { 
     System.out.println("getAndParseFile"); 
     String fileName=args[0]; 

     File file = new File(fileName); 

     File f2 = removeLineFromFile2(file.getAbsolutePath()); 
     File f1 = removeLineFromFile1(file.getAbsolutePath()); 
     File f = removeLineFromFile(file.getAbsolutePath()); 
     System.out.println("Finished Removing Lines"); 

     parse(f2); 
     parse(f1); 
     parse(f); 
    } 
} 

removeLineFromFile2代表你需要做什麼,removeLineFromFile1表示如果你正確地閱讀的東西會發生什麼,但寫他們在一個錯誤的方式(我懷疑是你的情況發生了什麼)和removeLineFromFile是你的實現,它在我的系統上什麼都不做。

getAndParseFile 
Temp file is: \path\to\sample-utf16.xml.2.tmp 
Temp file is: \path\to\sample-utf16.xml.1.tmp 
Finished Removing Lines 
Parsing \path\to\sample-utf16.xml.2.tmp 
Done with \path\to\sample-utf16.xml.2.tmp 
Parsing \path\to\sample-utf16.xml.1.tmp 
An exception occurred: Content is not allowed in prolog. 
Done with \path\to\sample-utf16.xml.1.tmp 
Parsing \path\to\sample-utf16.xml 
Done with \path\to\sample-utf16.xml 

以上所有假設您的輸入文件的確如UTF-16格式,如XML文件中所述。但我認爲情況並非如此。如果你自己創建了這個文件,那麼你做錯了。嘗試在Notepad ++(或類似工具)中打開它,並通過編碼菜單(應該說UCS-2或UTF-16,而不是ANSI,UTF-8等)檢查編碼。

您的代碼應該總是顯式指定它所期望的文件的編碼。

+0

xml文件是從我沒有維護的網站下載的,但我的任務是解析它。謝謝你向我解釋清楚。我也不知道它可能有不同於它在XML文件開頭指定的編碼,所以現在我明白瞭如何檢查它。學到了很多,謝謝。 – arsarc