Java - 写入XML文件时,除第一个元素外,其他所有元素都缩进了

4
使用JAVA,我正在尝试在打开.xml文件后,通过SWING应用程序追加创建一个新节点。每个新节点都被正确地输入,除了第一个元素总是卡在文件的最左边,没有缩进。 schedule.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<Schedule>
        <Lesson>
                <Title>Artificial Intelligence</Title>
                <Lecture>
                    <Day>Thursday</Day>
                </Lecture>
                <Professor>John Doe</Professor>
        </Lesson>
        <Lesson>
                <Title>Constraint Satisfaction Problems</Title>
                <Lecture>
                    <Day>Monday</Day>
                </Lecture>
        </Lesson>
</Schedule>

我的尝试写入文件:

try {
                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                    Document document = documentBuilder.parse("schedule.xml");
                    Element root = document.getDocumentElement();
                        Element newLesson = document.createElement("Lesson");

                        Element newTitle = document.createElement("Title");
                        newTitle.appendChild(document.createTextNode("myLesson"));
                        newLesson.appendChild(newTitle);

                        Element newLecture = document.createElement("Lecture");
                        newLesson.appendChild(newLecture);

                        Element newDay = document.createElement("Day");
                        newDay.appendChild(document.createTextNode("myDay"));
                        newLecture.appendChild(newDay);

                        Element newProfessor = document.createElement("Professor");
                        newProfessor.appendChild(document.createTextNode("myProfessor"));
                        newLesson.appendChild(newProfessor);

                        root.appendChild(newLesson);
                     DOMSource source = new DOMSource(document);
                     TransformerFactory transformerFactory = TransformerFactory.newInstance();
                     Transformer transformer = transformerFactory.newTransformer();
                     StreamResult result = new StreamResult("schedule.xml");
                     transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                     transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "8");
                     transformer.transform(source, result);
                } catch (Exception e) {
                    e.printStackTrace();
                }

输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Schedule>
        <Lesson>
                <Title>Artificial Intelligence</Title>
                <Lecture>
                    <Day>Thursday</Day>
                </Lecture>
                <Professor>John Doe</Professor>
        </Lesson>
        <Lesson>
                <Title>Constraint Satisfaction Problems</Title>
                <Lecture>
                    <Day>Monday</Day>
                </Lecture>
        </Lesson>
<Lesson>
            <Title>myLesson</Title>
            <Lecture>
                    <Day>myDay</Day>
            </Lecture>
            <Professor>myProfessor</Professor>
    </Lesson>
</Schedule>

它真的在缩进0处打印<Lesson>,并在缩进4处打印</Lesson>吗?您使用的是哪个Java版本? - Thomas Weller
由于存在一个错误:http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6296446 - Thomas Weller
Java版本:1.8.0_25 - ceid-vg
通过这个更改,我们的缩进为0。每个节点都在同一行(始终如此,对于新条目)。 - ceid-vg
这个问题在Java 9中似乎已经被修复了。 - VGR
显示剩余2条评论
1个回答

0
解决方案:使用一个函数从here中去除空格。

函数:

private static void removeEmptyText(Node node){
    Node child = node.getFirstChild();
    while(child!=null){
        Node sibling = child.getNextSibling();
        if(child.getNodeType()==Node.TEXT_NODE){
            if(child.getTextContent().trim().isEmpty())
                node.removeChild(child);
        }else
            removeEmptyText(child);
        child = sibling;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接