使用面向对象对XML进行解析:dom和dom4j的用法

时间:2022-11-28 02:46:17

这是一个比较简单的例子,主要是运用面向对象的思想,对XML文件进行解析,用dom和dom4j方法

<?xml version="1.0" encoding="utf-8"?>
<students>
<student>
<name>张三</name>
<age>20</age>
<school>北京大学</school>
</student>
<student>
<name>李四</name>
<age>18</age>
<school>清华大学</school>
</student>
</students>

代码如下:

学生对象类

package d20160624;

public class Student {
private String name;
private String age;
private String school; public Student() {
} public Student(String name, String age, String school) {
this.name = name;
this.age = age;
this.school = school;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getSchool() {
return school;
} public void setSchool(String school) {
this.school = school;
}
//打印学生信息
public void showInfo(){
System.out.println("姓名:"+this.name+"\n年纪:"+this.age+"\n学校:"+this.school);
System.out.println("***********");
} }

dom解析:

 package d20160624;

 import java.io.File;
import java.io.IOException; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; /*
* 使用DOM解析student.xml,输出打印学生信息
*/
public class DomParseStudent {
private Document document;
private String name,age,school; // 生成dom树
public void getDom(File file){
// 创建解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try {
// 创建解析器对象
DocumentBuilder db=dbf.newDocumentBuilder();
// 解析XML文件得到DOM树
this.document=db.parse(file);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
// 解析XML
public void showEle(){
DomParseStudent dps=new DomParseStudent();
NodeList students=document.getElementsByTagName("student");
for(int i=0;i<students.getLength();i++){
Element student=(Element) students.item(i);
NodeList names=student.getElementsByTagName("name");
for(int j=0;j<names.getLength();j++){
Element name=(Element) names.item(j);
this.name=name.getTextContent();
}
NodeList ages=student.getElementsByTagName("age");
for(int k=0;k<ages.getLength();k++){
Element age=(Element) ages.item(k);
this.age=age.getTextContent();
}
NodeList schools=student.getElementsByTagName("school");
for(int p=0;p<schools.getLength();p++){
Element school=(Element) schools.item(p);
this.school=school.getTextContent();
} Student stu=new Student(name, age, school);
stu.showInfo(); }
} public static void main(String[] args){
DomParseStudent dps=new DomParseStudent();
File file=new File("D:\\Java_study\\workspace\\myeclipse\\HomeWork\\src\\d20160624\\student.xml");
dps.getDom(file);
dps.showEle();
}
}

dom4j解析:

 package d20160624;

 import java.io.File;
import java.util.Iterator;
import java.util.List; import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element; /*
* 使用dom4j解析student.xml,输出打印学生信息
*/
public class Dom4jStudent {
private Document document;
private String name,age,school;
// 生成dom树
public void getDom(File file){
// 创建SAXReader
SAXReader sax=new SAXReader();
try {
// 读取xml文件,生成dom树
this.document=sax.read(file);
} catch (DocumentException e) {
e.printStackTrace();
}
} // 读取XML文件
public void showXML(){
Element root=document.getRootElement();
List<Element> student=root.elements();
for(Iterator<Element> stuit=student.iterator();stuit.hasNext();){
Element stu=stuit.next();
List<Element> names=stu.elements("name");
for(Iterator<Element> name=names.iterator();name.hasNext();){
Element nameel=name.next();
this.name=nameel.getText();
}
List<Element> ages=stu.elements("age");
for(Iterator<Element> age=ages.iterator();age.hasNext();){
Element ageel=age.next();
this.age=ageel.getText();
}
List<Element> schools=stu.elements("school");
for(Iterator<Element> school=schools.iterator();school.hasNext();){
Element schoolel=school.next();
this.school=schoolel.getText();
}
Student stu1=new Student(name, age, school);
stu1.showInfo();
}
} public static void main(String[] args){
Dom4jStudent d4j=new Dom4jStudent();
File file=new File("D:\\Java_study\\workspace\\myeclipse\\HomeWork\\src\\d20160624\\student.xml");
d4j.getDom(file);
d4j.showXML();
}
}

其实2种方式,都可以,都是各种包中类的方法的使用,会灵活运用就可以了!