Posted on 2009-07-03 17:28
胡娟 阅读(372)
评论(0) 编辑 收藏 所属分类:
JAVA
所需要jiar包
所要解析和创建的xml文件样式
<?xml version="1.0" encoding="gb2312"?>
<students>
<student id="1">
<name>hujuan</name>
<age>24</age>
</student>
<student id="2">
<name>fankai</name>
<age>24</age>
</student>
</students>
解析xml过程
package com.hujuan.xml;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class ReadXML {
public void readXml(String fileName){
//创建builder实例,这里的false表示使用默认的解析器
SAXBuilder builder = new SAXBuilder(false);
try {
//加载xml
Document document = builder.build(fileName);
//获得根元素
Element studentsElement = document.getRootElement();
//获得student元素下面的所有元素的子元素
List studentList = studentsElement.getChildren("student");
//循环list集合
for(Iterator it = studentList.iterator();it.hasNext();){
Element student = (Element)it.next();
//获得得元素的属性
String id = student.getAttributeValue("id");
System.out.println(id);
//获得子元素的属性
String name = student.getChildTextTrim("name");
System.out.println(name);
String age = student.getChild("age").getText();
System.out.println(age);
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
创建xml的过程
package com.hujuan.xml;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class CreateXml {
public void createXml(){
//创建一个Document,因为xml文档必须一直有一个唯一的根元素,所以Document将Element放在它的构造器中
Element studentsElement = new Element("students");
Document document = new Document(studentsElement);
//添加一个子元素,student节点
Element student = new Element("student");
//给student节点添加id属性,值为1
student.setAttribute(new Attribute("id","1"));
Element name = new Element("name");
name.addContent("hujuan");
student.addContent(name);
student.addContent(new Element("age").addContent("24"));
studentsElement.addContent(student);
Element student2 = new Element("student").setAttribute("id","2");
//添加子元素并设置元素内容
student2.addContent(new Element("name").setText("fankai"));
student2.addContent(new Element("age").setText("24"));
studentsElement.addContent(student2);
try {
//将xml输出到屏幕上
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(document, System.out);
//将xml输出到文件中
FileWriter writer = new FileWriter("src/aa.xml");
outputter.output(document, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试用例
package junit.test;
import org.junit.Test;
import com.hujuan.xml.CreateXml;
import com.hujuan.xml.ReadXML;
public class XmlTest {
private String fileName = "src/student.xml";
@Test
public void readXmlTest(){
ReadXML readXml = new ReadXML();
readXml.readXml(fileName);
}
@Test
public void creatXmlTest(){
CreateXml createXml = new CreateXml();
createXml.createXml();
}
}
调用readXmlTest()方法输出结果
1
hujuan
24
2
fankai
24
调用creatXmlTest()方法的输出结果
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="1">
<name>hujuan</name>
<age>24</age>
</student>
<student id="2">
<name>fankai</name>
<age>24</age>
</student>
</students>
修改元素节点的值
student.getChild("age").setText("22");
删除节点
student.removeChild("name")
Format 控制输出文档的格式
Format.getCompactFormat() 表示采用紧凑的格式
Format.getRawFormat() 表示采用原始格式
Format.getPrettyFormat() 表示采用美化格式,即以两个空格作为缩进来输出文档