SaxParseException - File的过早结束

时间:2015-07-06 08:25:06

标签: java xml io

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
      <execution>
        <id>default-jar</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        <configuration>
          <excludes>
            <exclude><!-- package you want to exclude --></exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
</plugin>

实际上这个问题解析(xml)。我得到错误信息“文件过早结束”第一条记录添加xmlfile.I无法找到真正的解决方案。请帮助我

        File xml = new File("C:/members.xml");
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory
                .newDocumentBuilder();
        documentBuilder = documentBuilderFactory.newDocumentBuilder();

        Document document = documentBuilder.parse(xml);


        document.getDocumentElement().normalize();

        NodeList nodeList = document.getElementsByTagName("member");

2 个答案:

答案 0 :(得分:0)

您的示例有问题 - 以下代码运行时没有错误:

public void test() throws ParserConfigurationException, SAXException, IOException {
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<members>"
            + "<member id=\"0\">"
            + "<name>a</name>"
            + "<surname>a</surname>"
            + "<fatherName>a</fatherName>"
            + "<age>0</age>"
            + "<email>a@a.com</email>"
            + "<username>a</username>"
            + "<password>a</password>"
            + "</member>"
            + "</members>";
    //File xml = new File("C:/members.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    Document document = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

    document.getDocumentElement().normalize();

    NodeList nodeList = document.getElementsByTagName("member");
}

答案 1 :(得分:0)

尝试在webbrowse中打开xml文件(firefox或chrome) 添加看看他们是否报告任何错误。

如果你有linux,你可以这样做:

xmllint members.xml

此命令告诉文件是否有效xml