2017-11-16 9 views
3

Javaを使用して、.xmlファイルを開いた後、SWINGアプリケーションを使用して新しいノードの作成を追加しようとしています。新しいノードはすべて、ファイルの一番左に常に付いている最初の要素を除き、正しく入力されます。Java - XMLファイルへの書き込みは、最初の要素以外のものすべてをインデントします。

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

それは本当にインデント0とインデント4で ''で '' を出力しますか?どのJavaバージョンを使用していますか? –

+0

バグがあったので、http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6296446 –

+0

Javaバージョン:1.8.0_25 –

答えて

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