簡體   English   中英

org.xml.sax.SAXParseException:文件過早結束

[英]org.xml.sax.SAXParseException: Premature end of file

我有這段代碼提供給SAXParseError即,它沒有獲取內容,但是當我在我的測試 pg 中使用它時,它工作正常。 在這里我提供我的原始代碼片段,any1 可以找出問題所在

這是I fetch a xml and append a node to it的部分,但似乎我無法獲取文件本身

String atemp=readFileAsString("../webapps/abc/include/xml/data.xml")

        log.info("ok thats good")

        String[] splitString=res.split(",")

        log.info(atemp)

        try
        {
            def root = new XmlSlurper().parseText(atemp)
        }
        catch(Exception e)
        {
            log.debug("parse errror: "+e)
        }
        root.appendNode {
          row {
            name(splitString[1])
            host(splitString[2])
            desc(splitString[3])
            product(splitString[4])
            type(splitString[5])
            time(splitString[6])
            by(splitString[7])
          }
        }

        def outputBuilder = new StreamingMarkupBuilder()
        String temp = outputBuilder.bind{ mkp.yield root }

        File file = new File("../webapps/abc/include/xml/data.xml");
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        output.write(temp);
        output.close();
        log.info(temp)

我的readFileAsString function

private String readFileAsString(String filePath) {
    try
    {
        StringBuffer fileData = new StringBuffer();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        log.info(reader==null);
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            log.info(readData)
        }
        reader.close();
        log.info("File Content\n"+fileData.toString());
        return fileData.toString();
    }
    catch (Exception e) 
    {
        log.info(e);
    }
}

Output

INFO http-5050-Processor24 com.abc.helper.WriteXml - false
INFO http-5050-Processor24 com.abc.helper.WriteXml - File Content
INFO http-5050-Processor24 com.abc.helper.WriteXml - ok thats good

注意:我可以在我的測試程序中使用相同的路徑打開文件

假設這是在 Groovy 中,我相信您可以將所有代碼替換為:

File f = new File( '../webapps/abc/include/xml/data.xml' )
def root = new XmlSlurper().parse( f )
String[] splitString = res.split( ',' )
root.appendNode {
  row {
    name(splitString[1])
    host(splitString[2])
    desc(splitString[3])
    product(splitString[4])
    type(splitString[5])
    time(splitString[6])
    by(splitString[7])
  }
}
def outputBuilder = new StreamingMarkupBuilder()
String newXml = outputBuilder.bind{ mkp.yield root }
f.text = newXml

但是我再說一遍,在 web 應用程序中執行此操作可能是一個壞主意,因為如果兩個線程同時調用此代碼,文件將是不可預測的(並且隨着文件變大變得更加不可預測)

此外,您可能想要更改:

File f = new File( '../webapps/abc/include/xml/data.xml' )

File f = new File( "${System.properties['catalina.base']}/webapps/abc/include/xml/data.xml" )

正如您在上一個問題中向您建議的那樣

readFileAsString方法看起來不錯,但如果相信日志 output 則您的應用程序正在讀取一個空文件,這就是導致 XML 解析異常的原因。

首先要檢查的是您嘗試讀取的文件的路徑名實際上是否正確,並且該位置的文件實際上不是空的。 嘗試在readFileAsString的開頭添加這一行

log.info("file size is " + new File(filePath).length());

請注意,零表示文件不存在或為空。


查看代碼的 rest ,您的應用程序似乎正在讀取文件,對其執行某些操作,然后將其寫回同一位置 考慮可能是生成 output 的代碼被破壞了......並且您的應用程序正在嘗試讀取和解析它之前破壞的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM