您的位置:首页 > 其它

JDOM生成、解析XML实例

2015-01-21 08:43 281 查看


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进行xml文档的写入操作
*/
public void createXml(File file) {

// 1.创建元素 及 设置为根元素
Element employees = newElement("employees");
document.setContent(employees);

// 2.创建注释 及 设置到根元素上
Comment commet = new Comment("thisis my comment");
employees.addContent(commet);

// 3.创建元素
Element element1 = newElement("employee");

// 3.1 设置元素的属性名及属性值
element1.setAttribute(newAttribute("id", "0001"));

// 3.2 创建元素的属性名及属性值
Attribute nameAttr = newAttribute("name", "wanglp");

// 3.3 设置元素名及文本
Element sexEle = newElement("sex");
sexEle.setText("m");
// 设置到上层元素上
element1.addContent(sexEle);

// 设置元素
Element ageEle = newElement("age");
ageEle.setText("22");
element1.addContent(ageEle);

// 设置为根元素的子元素
employees.addContent(element1);
// 将元素属性设置到元素上
element1.setAttribute(nameAttr);

// 3.创建元素
Element element2 = newElement("employee");

// 3.1 设置元素的属性名及属性值
element2.setAttribute(newAttribute("id", "0002"));

// 3.2 创建元素的属性名及属性值
Attribute name2Attr = newAttribute("name", "fox");

// 3.3 设置元素名及文本
Element sex2Ele = newElement("sex");
sex2Ele.setText("f");
// 设置到上层元素上
element2.addContent(sex2Ele);

// 设置元素
Element age2Ele = newElement("age");
age2Ele.setText("21");
element2.addContent(age2Ele);

// 设置为根元素的子元素
employees.addContent(element2);
// 将元素属性设置到元素上
element2.setAttribute(name2Attr);

Element element3 = new Element("employee");
element3.setText("title");
element3.addContent(newElement("name").addContent(new Element("hello")));
employees.addContent(element3);

// 设置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进行xml文档的读取操作
*/
public void parserXml(File file) {
// 建立解析器
SAXBuilder builder = new SAXBuilder();
try {
// 将解析器与文档关联
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>");
}

/**
* 测试
*/
public static void main(String[] args) {

JdomDemo jdom = new JdomDemo();
File file = newFile("E://jdom.xml");
jdom.createXml(file);
jdom.parserXml(file);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: