首先創建一個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..........................直接運行就可以了