2014-09-23 45 views
-1

好吧,接下來從我的最後一個問題,我已經創建了一個可擴展的XML API用於使用dom4j設置文件類的東西,我想知道是否有任何格式或其他方法在每個元素之間添加一個返回值。下面是該文件的創建和編寫腳本:(JAVA)在文檔中的元素之間添加返回

public static Document Compile(int loops, int[] attr, String[] data, String[][][] dataattr, String text[]) { 
     Document BetterDoc = DocumentHelper.createDocument(); 
     Element root = BetterDoc.addElement("Root"); 
     Element[] _data = new Element[loops]; 
     for (int i = 0; i < loops; i++) { 
      _data[i] = root.addElement(data[i]); 
      for (int i2 = 0; i2 < attr[i]; i2++) { 
       _data[i].addAttribute(dataattr[i][i2][0], dataattr[i][i2][1]); 
       if (text[i] != null || text[i] != "null") { 
        _data[i].addText(text[i]); 
       } 
      } 
     } 
     return BetterDoc; 
    } //END CreateDocument 
public static void Write(Document document, File input) throws IOException { 
     // lets write to a file 
     XMLWriter writer = new XMLWriter(new FileWriter(input)); 
     writer.write(document); 
     writer.close(); 
     // Pretty print the document to System.out 
     OutputFormat format = OutputFormat.createPrettyPrint(); 
     writer = new XMLWriter(System.out, format); 
     writer.write(document); 
     // Compact format to System.out 
     format = OutputFormat.createCompactFormat(); 
     writer = new XMLWriter(System.out, format); 
     writer.write(document); 
    } //END Write 

這是我目前正在使用的輸入測試數據:

public static void EXAMPLEWRITE() throws IOException { 
    int[] targAttr = new int[3]; 
    targAttr[0] = 0; 
    targAttr[1] = 1; 
    targAttr[2] = 2; 
    String[] ElementIn = new String[3]; 
    ElementIn[0] = "hello"; 
    ElementIn[1] = "yes"; 
    ElementIn[2] = "no"; 
    String[][][] AttrIn = new String[3][3][2]; 
    AttrIn[0][0][0] = null; 
    AttrIn[0][0][1] = null; 
    AttrIn[1][0][0] = "element2 attr1"; 
    AttrIn[1][0][1] = "true"; 
    AttrIn[2][0][0] = "element3 attr1"; 
    AttrIn[2][0][1] = "false"; 
    AttrIn[2][1][0] = "element3 attr2"; 
    AttrIn[2][1][1] = "false"; 
    String text[] = new String[3]; 
    text[0] = null; 
    text[1] = "element2 text"; 
    text[2] = "element3 text"; 

    Document d = Compile(3, targAttr, ElementIn, AttrIn, text); 
    File yes = new File("C:/Users/samuel.clark17/Desktop/hello.xml"); 
    Write(d, yes); 
} 

這是當前輸出我從我的代碼獲得:

<?xml version="1.0" encoding="UTF-8"?> 
<Root><hello/><yes element2 attr1="true">element2 text</yes><no element3 attr1="false" element3 attr2="false">element3 textelement3 text</no></Root><!-- 

,但我想它是這個樣子:

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
<hello/><yes element2 attr1="true">element2 text</yes> 
<no element3 attr1="false" element3 attr2="false">element3 textelement3 text</no> 
</Root><!-- 

,如果任何人都可以請幫我,我將非常感激,並與編譯器的輸入幫助這裏是我的超級特殊註釋哈哈:

/* 
* +++++++++++++++++++++++++++++++++++++++PIEMANSAM5++++++++++++++++++++++++++++++++++ 
* XML Settings/save file input output API 

* to write file a Document needs to be created, call the CreateObject object, 
* for correct use the following informations needs to be supplied in the parameters: 

* 1. loops (Integer) 
*  this is the amount of elements that branch out of the Root element 

* 2. attr (Integer array) 
*  this is the amount of attributes each element has under it inside. the first 
*  dimension is used to distinguish which element the attributes are being 
*  initialized. 

* 3.data[] 
*  this is the String array used to name the elements. the first dimension is to 
*  distinguish which element is being initialized. 

* 4.dataattr[][][] 
*  this is the data string used to create the attributes and insert what state the 
*  attribute is in, the first dimension is the element the attribute is under, 
*  the second dimension is the name of the attribute and the third dimension is 
*  the state of the dimension 

* 5. text[] 
*  text is the comment used upon the attribute. the first dimension is used to 
*  distinguish what element it is being added to. 

* +++++++++++++++++++++++++++++++++++++++PIEMANSAM5++++++++++++++++++++++++++++++++++ 
*/ 

在此先感謝的人誰可以幫助我的youre所有的傳說在這裏:D 愛你Piemansam5 xoxo 漂亮請不要投票這下來:(我已經盡了最大的努力,這個詞很好。

回答

0

改變了格式的放置和問題解決哈哈

public static void Write(Document document, File input) throws IOException { 
     // lets write to a file 
     OutputFormat format = OutputFormat.createPrettyPrint(); 
     XMLWriter writer = new XMLWriter(new FileWriter(input), format); 
     writer.write(document); 
     writer.close(); 
     // Pretty print the document to System.out 
     writer = new XMLWriter(System.out, format); 
     writer.write(document); 
     // Compact format to System.out 
     format = OutputFormat.createCompactFormat(); 
     writer = new XMLWriter(System.out, format); 
     writer.write(document); 
    } //END Write 
相關問題