jdom和dom4j的使用实例

用jdom对xml文件进行读写。

首先要下载一个jdom的jar包。再自己的项目中导入相应的jar包,其中包括builder目录下的jdom.jar,还是就是lib下面的jar包。。接下来就是操作了:(我的xml文件和class文件都放在了同一个目录下了)
1.xml文件实例:

<?xml version="1.0" encoding="UTF-8"?>
<school>
<class name="class2">
<student>
<name>古天乐</name>
<age>30</age>
<score>59.3</score>
</student>
<student>
<name>刘德华</name>
<age>35</age>
<score>59.6</score>
</student>
</class>
<class name="class3">
<student>
<name>张学友</name>
<age>36</age>
<score>59.8</score>
</student>
</class>
</school>

2.对xml进行操作的java文件:

import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


public class ReadXML4JDOM {

public static void main(String[] args) {
read("myschool.xml");
System.out.println("执行完毕!");
}

public static void read(String filename) {
SAXBuilder saxb = new SAXBuilder();
try {
Document dc = saxb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename));
Element root = dc.getRootElement();
List<Element> classes = (List) root.getChildren("class");
for(Element element : classes) {
String className = element.getAttributeValue("name");
System.out.println(className+":");
if(((List)element.getChildren("student")).size() == 1) {
Element sudent = element.getChild("student");
String name = sudent.getChildText("name");
String age = sudent.getChildText("age");
String score = sudent.getChildText("score");
System.out.println("My name is "+name+",I'm "+age+" old! I'm in "+className+",and I got "+score+" scores!");
}else {
for(Element student : (List<Element>)element.getChildren("student")) {
String name = student.getChildText("name");
String age = student.getChildText("age");
String score = student.getChildText("score");
System.out.println("My name is "+name+",I'm "+age+" old! I'm in "+className+",and I got "+score+" scores!");
}
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}



3。运行结果:

class2:
My name is 古天乐,I'm 30 old! I'm in class2,and I got 59.3 scores!
My name is 刘德华,I'm 35 old! I'm in class2,and I got 59.6 scores!
class3:
My name is 张学友,I'm 36 old! I'm in class3,and I got 59.8 scores!
执行完毕!
---------------------------------------------------------
---------------------下面是dom4j---------------------------
----------------------------------------------------------
1.首先引入包。dom4j.jar,如果需要的话,还可以引入jaxen-。。-beta.jar

2.xml。和jdom的是一样的:


<?xml version="1.0" encoding="UTF-8"?>
<school>
<class name="class2">
<student>
<name>古天乐</name>
<age>30</age>
<score>59.3</score>
</student>
<student>
<name>刘德华</name>
<age>35</age>
<score>59.6</score>
</student>
</class>
<class name="class3">
<student>
<name>张学友</name>
<age>36</age>
<score>59.8</score>
</student>
</class>
</school>

3.java代码:


import java.io.File;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class ReadXML4Dom4j {

public static void main(String[] args){
read("src/myschool.xml");
}

public static void read(String filename) {
SAXReader sr = new SAXReader();
try {
Document dc = sr.read(new File(filename));
//获取根元素
Element root = dc.getRootElement();
for(Object obj : root.elements()) {
Element myclass = (Element)obj;
//获取某个标签的属性,首先获得标签的属性,然后再获取内容
Attribute ab = myclass.attribute("name");
String classname = ab.getText();
System.out.println(classname+":");
//获取myclass标签下面的所有子元素
List<Element> students = (List<Element>)myclass.elements();

if(students.size() == 1) {
Element student = students.get(0);
//获取name标签
Element nameEle = student.element("name");
//获取name标签的内容
String name = nameEle.getText();

Element ageEle = student.element("age");
String age = ageEle.getText();

Element scoreEle = student.element("score");
String score = scoreEle.getText();

System.out.println("name: "+name+",age: " +age+", score: "+score);

}else {
for(Element student : students) {
Element nameEle = student.element("name");
String name = nameEle.getText();

Element ageEle = student.element("age");
String age = ageEle.getText();

Element scoreEle = student.element("score");
String score = scoreEle.getText();
System.out.println("name: "+name+",age: " +age+", score: "+score);
}
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值