import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class CreateXML {
private Element customer,name,age,sex,address,street,city,district;
public CreateXML(){
customer = new Element("customer");
Document document = new Document(customer);
name = new Element("name");
age = new Element("age");
sex = new Element("sex");
address = new Element("address");
street = new Element("street");
city = new Element("city");
district = new Element("district");
name.addContent("张旭");
age.addContent("25");
sex.addContent("男");
city.addContent("无锡市");
street.addContent("清扬路");
district.addContent("沁园新村370号");
address.addContent(street);
address.addContent("city");
address.addContent("district");
customer.addContent(name);
customer.addContent(age);
customer.addContent(sex);
customer.addContent(address);
XMLOutputter fmt = new XMLOutputter();
try {
FileWriter writer = new FileWriter("c:/customer.xml");
Format f = Format.getPrettyFormat();
f.setEncoding("GB2312");
fmt.setFormat(f);
fmt.output(document, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
new CreateXML();
}
}
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class CreateXML {
private Element customer,name,age,sex,address,street,city,district;
public CreateXML(){
customer = new Element("customer");
Document document = new Document(customer);
name = new Element("name");
age = new Element("age");
sex = new Element("sex");
address = new Element("address");
street = new Element("street");
city = new Element("city");
district = new Element("district");
name.addContent("张旭");
age.addContent("25");
sex.addContent("男");
city.addContent("无锡市");
street.addContent("清扬路");
district.addContent("沁园新村370号");
address.addContent(street);
address.addContent("city");
address.addContent("district");
customer.addContent(name);
customer.addContent(age);
customer.addContent(sex);
customer.addContent(address);
XMLOutputter fmt = new XMLOutputter();
try {
FileWriter writer = new FileWriter("c:/customer.xml");
Format f = Format.getPrettyFormat();
f.setEncoding("GB2312");
fmt.setFormat(f);
fmt.output(document, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
new CreateXML();
}
}