创建并写入XML:
复制内容到剪贴板 程序代码
package xmltest;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class CreateXML {
public void Create() {
try {
Document doc = new Document();
ProcessingInstruction pi = new ProcessingInstruction(
"xml-stylesheet", "type='text/xsl' href='test.xsl'");
doc.addContent(pi);
Element root = new Element("user");
doc.setRootElement(root);
Element el1 = new Element("name");
//Text text1 = new Text("blurxx");
Element em = new Element("name_last").addContent("blurxx");
el1.addContent(em);
Element el2 = new Element("passwd").addContent("1234");
root.addContent(el1);
root.addContent(el2);
//缩进四个空格,自动换行,gb2312编码
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class CreateXML {
public void Create() {
try {
Document doc = new Document();
ProcessingInstruction pi = new ProcessingInstruction(
"xml-stylesheet", "type='text/xsl' href='test.xsl'");
doc.addContent(pi);
Element root = new Element("user");
doc.setRootElement(root);
Element el1 = new Element("name");
//Text text1 = new Text("blurxx");
Element em = new Element("name_last").addContent("blurxx");
el1.addContent(em);
Element el2 = new Element("passwd").addContent("1234");
root.addContent(el1);
root.addContent(el2);
//缩进四个空格,自动换行,gb2312编码
Tags: XML