首先创建一个UserBean对象
package com.Stu;
public class Stu {
private String id;
private String add;
private String name;
private String sex;
private int age;
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "id:"+this.id+"\tadd:"+this.add+"\tname:"+this.name+"\tsex:"+this.sex+"\tage:"+this.age;
}
}
2.......导入jdom.jar架包
3............................读取并创建xml
package com.Stu;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.ProcessingInstruction;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* 此类功能:用于把程序中的数据保存到XML文件中
* @author
*/
public class TestWriteXml {
/**
* 程序主入口方法
* @param args
*/
public static void main(String[] args) {
//实例输出流
XMLOutputter out = new XMLOutputter();
//得到格式化对象
Format format = Format.getPrettyFormat();
//输出流应用格式化对象
out.setFormat(format);
//把XML写到硬盘上
try {
TestWriteXml path = new TestWriteXml();
System.out.println(path.getPath()+"/load/"+"stu.xml");
//使用XMLOutputter的output方法持久化一个名为“stu.xml”的磁盘文件
out.output(xmldoc(), new FileOutputStream(path.getPath()+"/load/"+"stu.xml"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建xml文件
public static Document xmldoc(){
//创建实例一个List集合,保存程序中的数据
List<Stu> list = new ArrayList<Stu>();
//调用setValue(list)方法往集合中添加数据
setValue(list);
//创建实例一个Document对象保存内存文档对象
Document document = new Document();
Map map = new HashMap();
map.put("type", "text/xsl");
map.put("href","products.xsl");
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet",map);//处理指令
//将处理指令添加
document.addContent(pi);
/*
* 创建文档类型并添加到文档
*/
DocType type = new DocType("taglib"); //文档类型
type.setSystemID("http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"); //设为 system
//添加文档类型
document.addContent(type);
//创建一个名为"stusEleRoot"的根节点元素
Element stusEleRoot = new Element("stus");
//循环遍历集合中的数据,添加到根元素节点中
for (Stu stu : list) {
//创建第二子节点stu,保存每个学生的信息
Element eleStu = new Element("stu");
//添加名为"ID"的属性到stu节点中
eleStu.setAttribute("ID", stu.getId());
//创建名为"name"的子节点元素,并把STU对象的"name"属性值设置为节点元素的值,然后添加到“stu”学生节点下
Element eleName = new Element("name");
eleName.setText(stu.getName());
eleStu.addContent(eleName);
//创建名为"add"的子节点元素,并把STU对象的"add"属性值设置为节点元素的值,然后添加到“stu”学生节点下
Element eleAdd = new Element("add");
eleAdd.setText(stu.getAdd());
eleStu.addContent(eleAdd);
//创建名为"sex"的子节点元素,并把STU对象的"sex"属性值设置为节点元素的值,然后添加到“stu”学生节点下
Element eleSex = new Element("sex");
eleSex.setText(stu.getSex());
eleStu.addContent(eleSex);
//创建名为"age"的子节点元素,并把STU对象的"age"属性值设置为节点元素的值,然后添加到“stu”学生节点下
Element eleAge = new Element("age");
eleAge.setText(stu.getAge() + "");//转类型(javaBean中的stu.getAge()值为integer类型)
eleStu.addContent(eleAge);
//将“eleStu”元素作为子节点添加到“eleRoot”根节点下
stusEleRoot.addContent(eleStu);
}
//添加根节点到内存文档对象中
document.setRootElement(stusEleRoot);
return document;
}
/**
* 程序启动时,往集合中循环添加10个元素,以模拟程序中的数据
* @param list集合
*/
private static void setValue(List<Stu> list) {
for (int i = 0; i < 10; i++) {
Stu stu = new Stu();
stu.setAdd("地址" + (i + 1));
stu.setAge((i + 21));
stu.setId("s" + (i + 1));
stu.setName("姓名" + (i + 1));
stu.setSex("男");
list.add(stu);
}
}
//获取工程的根路径
public String getPath(){
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
String rootpath = path.substring(0,path.indexOf("/WEB-INF/classes"));
return rootpath;
}
}
4..............................运行就OK了,很好理解
5....................另外给一种创建方式
package com.jdom;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Attribute;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.ProcessingInstruction;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class TranXMl {
public static void main(String[] args) throws IOException {
//创建空白文档
Document doc = new Document();
Map map = new HashMap();
map.put("type", "text/xsl");
map.put("href","products.xsl");
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet",map);//处理指令
//将处理指令添加
doc.addContent(pi);
/*
* 创建文档类型并添加到文档
*/
DocType type = new DocType("taglib"); //文档类型
type.setPublicID("http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"); //设为 system
//添加文档类型
doc.addContent(type);
Element root = new Element("productsDetails"); //创建一个元素
doc.setRootElement(root); //将该元素做为根元素
Element product = new Element("product");
root.addContent(product); //将product做为productsDetails的子元素
Attribute att = new Attribute("productID","0001"); //创建属性
product.setAttribute(att); //为product设置属性
//为product创建子元素,并将其content分别设为100.00,red
product.addContent(new Element("rate").setText("100.00"));
product.addContent(new Element("color").setText("红色"));
/*
* 格式化输出
*/
XMLOutputter outp = new XMLOutputter();//用于输出jdom 文档
Format format=Format.getPrettyFormat(); //格式化文档
format.setEncoding("utf-8"); //由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk
outp.setFormat(format);
outp.output(doc,System.out); //输出文档
}
}
6..........................直接运行就可以了