日期:2014-05-20  浏览次数:20841 次

用jdom生成 xml文件的问题
大家好   ,我想生成一个像下面格式的xml文件
<!--
    注释区域

-->


<Context       节点

</Context>

现在问题是我不知道怎么在一开始就加注释,还有不需要 <?xml   version= "1.0 "

encoding= "GBK "?>   这个开头。怎么做啊   谢谢!!


------解决方案--------------------
public class CreateXML {
public static void createXML() {
Element root = new Element( "Context ");
Document doc = new Document(root);
root.addContent( "node ");
//root.addContent(root);
StringBuffer strbuf = new StringBuffer();
try {

XMLOutputter outputter = new XMLOutputter();
FileWriter writer = new FileWriter( "c:\\test.xml ");
outputter.output(doc, writer);
writer.close();
strbuf.append( " <!-- " +
"--> ");
RandomAccessFile rf = new RandomAccessFile( "c:\\test.xml ", "rw ");
rf.write( "\r\n ".getBytes());
rf.write(strbuf.toString().getBytes());
rf.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
CreateXML.createXML();
}
}