JDOM生成、解析XML實(shí)例
來源:程序員人生 發(fā)布時(shí)間:2015-01-26 09:28:10 閱讀次數(shù):2865次
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
*
* jdom生成與解析XML文檔
*
*/
public class JdomDemo{
Document document = new Document();
/**
* 利用JDom進(jìn)行xml文檔的寫入操作
*/
public void createXml(File file) {
// 1.創(chuàng)建元素 及 設(shè)置為根元素
Element employees = newElement("employees");
document.setContent(employees);
// 2.創(chuàng)建注釋 及 設(shè)置到根元素上
Comment commet = new Comment("thisis my comment");
employees.addContent(commet);
// 3.創(chuàng)建元素
Element element1 = newElement("employee");
// 3.1 設(shè)置元素的屬性名及屬性值
element1.setAttribute(newAttribute("id", "0001"));
// 3.2 創(chuàng)建元素的屬性名及屬性值
Attribute nameAttr = newAttribute("name", "wanglp");
// 3.3 設(shè)置元素名及文本
Element sexEle = newElement("sex");
sexEle.setText("m");
// 設(shè)置到上層元素上
element1.addContent(sexEle);
// 設(shè)置元素
Element ageEle = newElement("age");
ageEle.setText("22");
element1.addContent(ageEle);
// 設(shè)置為根元素的子元素
employees.addContent(element1);
// 將元素屬性設(shè)置到元素上
element1.setAttribute(nameAttr);
// 3.創(chuàng)建元素
Element element2 = newElement("employee");
// 3.1 設(shè)置元素的屬性名及屬性值
element2.setAttribute(newAttribute("id", "0002"));
// 3.2 創(chuàng)建元素的屬性名及屬性值
Attribute name2Attr = newAttribute("name", "fox");
// 3.3 設(shè)置元素名及文本
Element sex2Ele = newElement("sex");
sex2Ele.setText("f");
// 設(shè)置到上層元素上
element2.addContent(sex2Ele);
// 設(shè)置元素
Element age2Ele = newElement("age");
age2Ele.setText("21");
element2.addContent(age2Ele);
// 設(shè)置為根元素的子元素
employees.addContent(element2);
// 將元素屬性設(shè)置到元素上
element2.setAttribute(name2Attr);
Element element3 = new Element("employee");
element3.setText("title");
element3.addContent(newElement("name").addContent(new Element("hello")));
employees.addContent(element3);
// 設(shè)置xml文檔輸出的格式
Format format =Format.getPrettyFormat();
XMLOutputter out = newXMLOutputter(format);
// 將得到的xml文檔輸出到文件流中
try {
out.output(document, newFileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 利用JDom進(jìn)行xml文檔的讀取操作
*/
public void parserXml(File file) {
// 建立解析器
SAXBuilder builder = new SAXBuilder();
try {
// 將解析器與文檔關(guān)聯(lián)
document = builder.build(file);
} catch (JDOMException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
// 讀取根元素
Element root =document.getRootElement();
// 輸出根元素的名字
System.out.println("<" +root.getName() + ">");
// 讀取元素集合
List<?> employeeList =root.getChildren("employee");
for (int i = 0; i <employeeList.size(); i++) {
Element ele = (Element) employeeList.get(i);
// 得到元素的名字
System.out.println("<"+ ele.getName() + ">");
// 讀取元素的屬性集合
List<?> empAttrList =ele.getAttributes();
for (int j = 0; j <empAttrList.size(); j++) {
Attribute attrs = (Attribute)empAttrList.get(j);
// 將屬性的名字和值 并 輸出
String name = attrs.getName();
String value = (String)attrs.getValue();
System.out.println(name +"=" + value);
}
try {
Element sex =ele.getChild("sex");
System.out.println("<sex>" + sex.getText());
Element age =ele.getChild("age");
System.out.println("<age>" + age.getText());
} catch (NullPointerException e) {
System.out.println(ele.getTextTrim());
Element name =ele.getChild("name");
System.out.println("<name>" + name.getName());
}
System.out.println("</employee>");
}
System.out.println("</employees>");
}
/**
* 測(cè)試
*/
public static void main(String[] args) {
JdomDemo jdom = new JdomDemo();
File file = newFile("E://jdom.xml");
jdom.createXml(file);
jdom.parserXml(file);
}
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)