繼續前天未整理完的API。當用Dom4j的API得到Document之後,還需要將document輸出,或者輸出到文件上,或者輸出到控制檯,或者網絡傳輸到另一臺機器上。
org.dom4j.io.XMLWriter
構造函數 XMLWriter(OutputStream out,OutputFormat format)
org.dom4j.io.XMLWriter
void write(Document doc)
打印Document到當前的Writer
org.dom4j.io.XMLWriter
void close()
關閉當前的Writer
org.dom4j.io.OutputFormat
static OutputFormat createPrettyPrint()
靜態方法返回OutputFormat實例(輸出格式爲pretty形式,就是格式比較整齊的那種)
官方文檔的示例程序如下,加上我的一點註釋:
public class Foo(){
public void write(Document document) throws IOException{
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));//輸出到文件
writer.write(document);
writer.close();
OutputFormat format = OutputFormat.createPrettyPrint();//格式爲美觀形式
writer = new XMLWriter(System.out,format);//writer的目的地是控制檯
writer.close();
format = OutputFormat.createCompactFormat();//格式爲緊湊的格式
writer = new XMLWriter(System.out,format);
writer.close();
}
}