簡體   English   中英

Java說XML文檔不成形

[英]Java saying XML Document Not 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應用程序來管理todo列表,該列表可以讀取和寫入定義任務的XML文件。

org.xml.sax.SAXParseException:根元素后面的文檔中的標記必須格式正確。

此特殊異常表示XML文檔中有多個根元素。 換句話說, <tasklist>不是唯一的根元素。 以XML文檔為例,考慮一個沒有<tasklist>元素且根目錄中有三個<task>元素的元素。 這會導致這種異常。

由於您發布的XML文件看起來很好,問題出在其他地方。 看起來它沒有解析你期望解析的XML文件。 要進行快速調試,請將以下內容添加到方法的頂部:

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

在磁盤文件系統中找到該文件並進行驗證。

我認為實際文件可能有問題。 當我復制你的代碼但使用XML作為解析器的字符串輸入時,它工作正常(在修復了幾個問題之后 - attributes.item(i)應該是attributes.item(j) ,你需要打破循環當attribute == null )時。

在嘗試重現您的錯誤時,如果我添加另一個<tasklist></tasklist>元素,我可以得到相同的消息。 這是因為XML不再具有單個根元素(tasklist)。 這是你看到的問題嗎? tasks.xml的XML是否只有一個根元素?

嘗試將XML聲明更改為:

<?xml version="1.0" encoding="UTF-8" ?>

為了它的價值,Scala REPL成功解析了你的標記。

scala> val tree = <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>
tree: scala.xml.Elem = 
<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>

另一個值得一提的是,這是我將xml保存到名為test.xml的文件並通過xmllint運行時得到的結果

[jhr@Macintosh] [~]
xmllint test.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>

似乎很好。 很可能你有一些你在實際文件中某處看不到的流浪角色。 嘗試在編輯器中查看實際文件,該編輯器將顯示不可打印的字符,就像其他人建議的那樣,如果這不是英文UTF-8計算機,您可能會有一些您無法看到解析器的Unicode字符。 那或你沒有加載你認為你的文件。 步驟調試,看看文件的實際內容在被送入解析器之前是什么。

你確定那個文件中的所有內容嗎? 錯誤是抱怨當前root之后有更多標記。 所以在</tasklist>之后必須有其他東西。

有時,此錯誤可能是由不可打印的字符引起的。 如果您沒有看到任何內容,請執行文件的hexdump。

暫無
暫無

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

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