Java 说 XML 文档格式不正确

标签 java xml parsing xmldocument non-well-formed

Java 的 XML 解析器似乎认为我的 XML 文档在根元素之后的格式不正确。但我已经用几种工具验证了它,但他们都不同意。这可能是我的代码错误,而不是文档本身的错误。如果你们能给我提供任何帮助,我将不胜感激。

这是我的 Java 方法:

private void loadFromXMLFile(File f) throws ParserConfigurationException, IOException, SAXException {
    File file = f;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    Document doc = null;
    db = dbf.newDocumentBuilder();
    doc = db.parse(file);
    doc.getDocumentElement().normalize();
    String desc = "";
    String due = "";
    String comment = "";
    NodeList tasksList = doc.getElementsByTagName("task");
    for (int i = 0; i  tasksList.getLength(); i++) {
        NodeList attributes = tasksList.item(i).getChildNodes();
        for (int j = 0; i < attributes.getLength(); j++) {
        Node attribute = attributes.item(i);
        if (attribute.getNodeName() == "description") {
            desc = attribute.getTextContent();
        }
        if (attribute.getNodeName() == "due") {
            due = attribute.getTextContent();
        }
        if (attribute.getNodeName() == "comment") {
            comment = attribute.getTextContent();
        }
        tasks.add(new Task(desc, due, comment));
        }
        desc = "";
        due = "";
        comment = "";
    }
}

以下是我要加载的 XML 文件:

<?xml version="1.0"?>  
<tasklist>  
    <task>  
        <description>Task 1</description>  
        <due>Due date 1</due>  
        <comment>Comment 1</comment>  
        <completed>false</completed>  
    </task>  
    <task>  
        <description>Task 2</description>  
        <due>Due date 2</due>  
        <comment>Comment 2</comment>  
        <completed>false</completed>  
    </task>  
    <task>  
        <description>Task 3</description>  
        <due>Due date 3</due>  
        <comment>Comment 3</comment>  
        <completed>true</completed>  
    </task>  
</tasklist>

这是 java 为我抛出的错误消息:

run:
[Fatal Error] tasks.xml:28:3: The markup in the document following the root element must be well-formed.
May 17, 2010 6:07:02 PM todolist.TodoListGUI <init>
SEVERE: null
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
        at todolist.TodoListGUI.loadFromXMLFile(TodoListGUI.java:199)
        at todolist.TodoListGUI.<init>(TodoListGUI.java:42)
        at todolist.Main.main(Main.java:25)
BUILD SUCCESSFUL (total time: 19 seconds)

引用TodoListGUI.java:199是

doc = db.parse(file);

如果上下文对这里的任何人都有帮助,我正在尝试编写一个简单的 GUI 应用程序来管理一个待办事项列表,该列表可以读取和写入定义任务的 XML 文件。

最佳答案

org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.

这一特殊异常表明 XML 文档中有多个根元素。换句话说,<tasklist>不是唯一的根元素。以您的 XML 文档为例,考虑一个没有 <tasklist> 的文档。元素和三个 <task>根中的元素。这会导致这种异常。

由于您发布的 XML 文件看起来很好,问题出在其他地方。看起来它没有解析您期望它正在解析的 XML 文件。为了快速调试,将以下内容添加到您的方法顶部:

System.out.println(f.getAbsolutePath());

在磁盘文件系统中定位文件并验证。

关于Java 说 XML 文档格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2853242/

相关文章:

c# - 读取 XML 的多个部分

java - 如何根据日期输入(edittext)查看数据?

python - 在python中读写cfg文件

python - 使用 PETL 解析 XML

java - 仅返回 SQLite Select 语句中的最后一个值

java - 我的 JDialog 有时会收到来自调用应用程序的多余按键? (提供代码)

java - Java 中 XML 解析和 WS API 的历史?

java - 读取包含两个整数列的TXT文件并保存到java中的两个数组中

java - SWT在数据库中保存数据的问题

java - 如何导入 LibGdx 示例项目