Java - 写入XML文件会缩进除第一个元素之外的所有内容

时间:2017-11-16 14:49:41

标签: java xml swing

使用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>

1 个答案:

答案 0 :(得分: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;
    }
}