static Format getPrettyFormat() Returns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. 主要是用来格式化输出的
package jdomdemo;
import org.jdom.*;
import org.jdom.output.*;
import java.io.*;
public class TestJDom3 { public static void main(String[] args) {
Element root=new Element("rootm");
Document myDocument=new Document(root);
Element a = new Element("a");
a.addContent("1");
Element b = new Element("b");
b.addContent("2");
Element c = new Element("c");
Element d = new Element("d");
d.addContent("3");
c.addContent(d);
Element e = new Element("e");
e.setAttribute("v","5");
e.setAttribute("l","6");
e.addContent("4");
Element f = new Element("f");
Element g = new Element("g");
g.setAttribute("t","7");
g.addContent("5");
f.addContent(g);
root.addContent(a);
root.addContent(b);
root.addContent(c);
root.addContent(e);
root.addContent(f);
try {
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(myDocument, System.out);
outputter.output(myDocument, new FileOutputStream("myFile.xml"));
} catch (java.io.IOException ee) {
ee.printStackTrace();
}
}
}
你的setAttribute有这个函数:setAttribute(String,String);
你的jdom包是多少版本的
?
Format是一个Jdom中的类,Format.getPrettyFormat()就可以完成换行。
的方法,读就是了。跟写差别不大