(JAVA)在文档中的元素之间添加返回

时间:2014-09-23 13:00:41

标签: java xml documents dom4j

好的,从我上一个问题开始,我已经使用dom4j为设置文件创建了一个可扩展的XML API,我想知道是否有任何格式或其他方法在每个元素之间添加一个返回。下面是文档创建和编写脚本:

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++++++++++++++++++++++++++++++++++
 */

提前感谢任何可以帮助我解决所有传说的人:D 爱你Piemansam5 xoxo 相当请不要投票这个:(我已经尽力把这个说得好。

1 个答案:

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