1

のデフォルトの動作はElement.toXML()、結果を 1 行で表示しているようです。結果を階層的に複数行で表示することは可能ですか?

例:

これは私が手に入れたいものです

<root>
    <Fraction hash="108e898f" />
    <Integer hash="19498483" />
</root>

そして、これは私が現時点で得ているものです:

<root><Fraction hash="108e898f" /><Integer hash="19498483" /></root>

ありがとう

4

2 に答える 2

2

nu.xom.Serializerまさにあなたが必要とするものです。使用は次のとおりです。

public static void main(String[] args) {
    Element root = new Element("root");  
    Element fraction = new Element("Fraction");
    fraction.addAttribute(new Attribute("hash", "108e898f"));
    root.appendChild(fraction);
    Element integer = new Element("Integer");
    integer.addAttribute(new Attribute("hash", "19498483"));
    root.appendChild(integer);
    Document doc = new Document(root);
    try {
        Serializer serializer = new Serializer(System.out, "ISO-8859-1");
        serializer.setIndent(4);
        serializer.setMaxLength(64);
        serializer.write(doc);  
    } catch (IOException ex) {
        System.err.println(ex); 
    }  
}
于 2011-12-11T22:37:39.043 に答える
1

きれいな印刷出力が必要なようです。Xomでそれを行うのは簡単なはずです、この前の答えを試してください、それは役に立つかもしれません。

于 2011-12-11T22:37:56.673 に答える