Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android上解析xml文件时文件过早结束_Java_Android_Xml_Xml Parsing_Xom - Fatal编程技术网

Java 在android上解析xml文件时文件过早结束

Java 在android上解析xml文件时文件过早结束,java,android,xml,xml-parsing,xom,Java,Android,Xml,Xml Parsing,Xom,我正在尝试从android应用程序上读取一个xml文件,用作xml库。我正在尝试这个: Builder parser = new Builder(); Document doc = parser.build(context.openFileInput(XML_FILE_LOCATION)); 但我得到了nu.xom.ParsingException:文件过早结束。即使文件是空的 我需要解析一个非常简单的XML文件,我准备使用另一个库而不是XOM,所以请告诉我是否有更好的库。或者只是使用XOM解

我正在尝试从android应用程序上读取一个xml文件,用作xml库。我正在尝试这个:

Builder parser = new Builder();
Document doc = parser.build(context.openFileInput(XML_FILE_LOCATION));
但我得到了nu.xom.ParsingException:文件过早结束。即使文件是空的

我需要解析一个非常简单的XML文件,我准备使用另一个库而不是XOM,所以请告诉我是否有更好的库。或者只是使用XOM解决问题

如果有帮助的话,我将使用它来获取解析器

---编辑---

PS:这样做的目的不是解析一个空文件,文件在第一次运行时正好是空的,这显示了这个错误。

如果您一直关注post,这似乎与xerces以及它是一个空文件这一事实有关,而且他们在xerces方面没有达成解决方案

因此,我按如下方式处理了这个问题:

Document doc = null;
try {
    Builder parser = new Builder();
    doc = parser.build(context.openFileInput(XML_FILE_LOCATION));
}catch (ParsingException ex) { //other catch blocks are required for other exceptions.
   //fails to open the file with a parsing error.
   //I create a new root element and a new document.
   //I fill them with xml data (else where in the code) and save them.
    Element root = new Element("root");
    doc = new Document(root);       
}
然后我可以对医生做任何我想做的事。您还可以添加额外的检查,以确保原因确实是一个空文件,如sam对该问题的评论中所示,检查文件大小。

如果您一直关注文章的结尾,这似乎与xerces和它是一个空文件这一事实有关,而且他们在xerces方面没有达成解决方案

因此,我按如下方式处理了这个问题:

Document doc = null;
try {
    Builder parser = new Builder();
    doc = parser.build(context.openFileInput(XML_FILE_LOCATION));
}catch (ParsingException ex) { //other catch blocks are required for other exceptions.
   //fails to open the file with a parsing error.
   //I create a new root element and a new document.
   //I fill them with xml data (else where in the code) and save them.
    Element root = new Element("root");
    doc = new Document(root);       
}

然后我可以对医生做任何我想做的事。您还可以添加额外的检查,以确保原因确实是一个空文件,如sam对该问题的一条评论所示,检查文件大小。

空文件不是格式良好的XML文档。在这里抛出ParsingException是正确的做法

空文件不是格式良好的XML文档。在这里抛出ParsingException是正确的做法

XML文件中的数据是什么?看起来你至少缺少了一个结束语。对不起,我更倾向于关注代码而不是口头描述。解析空文件似乎很愚蠢。也许您应该在解析文件之前检查文件的大小。XML文件中的数据是什么?看起来你至少缺少了一个结束语。对不起,我更倾向于关注代码而不是口头描述。解析空文件似乎很愚蠢。也许您应该在尝试解析文件之前检查文件的大小。我确信其他场景也可能引发ParsingException,而不是捕获异常,我想知道,以某种方式检查xml文件是否为“空”是否不那么难看。也许您可以检查底层FileChannel的名称,或FileInputStream中的字节?Whups,不确定我是如何忽略的。:/只是想确保你知道一种更“干净”的方法。。Sry,我没有时间编写干净的方法,当时是凌晨3点&我不想让这个问题悬而未决。我确信其他场景也可能引发ParsingException,而不是捕获异常,我想知道以某种方式检查xml文件是否为“空”是否不那么难看。也许您可以检查底层FileChannel的名称,或FileInputStream中的字节?Whups,不确定我是如何忽略的。:/只是想确保你知道一种更“干净”的方法。。Sry,我没有时间写干净的方法,当时是凌晨3点&我不想让这个问题没有答案。