4

Using JAVA, I am trying, after having opened a .xml file, to append the creation of a new node using a SWING Application. Every new node gets entered correctly EXCEPT the first element which always get stuck at the far left of the file, with no identation.

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>

My attempt to write to the file :

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();
                }

Output

<?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>
ceid-vg
  • 323
  • 2
  • 9
  • 21

1 Answers1

0

Solution: used a function for space trimming from here

Function:

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;
    }
}
ceid-vg
  • 323
  • 2
  • 9
  • 21