XML的解析(读和写)

1)使用dom4j需要导入dom4j对应的jar包:dom4j-1.6.1.jar
2)创建SAXReader类的对象来实现读取XML文档,代码如下:
SAXReader reader = new SAXReader();
3)使用SAXReader类的read方法获取Document对象,Document 对象是一棵XML文档树的对象,可为我们提供对文档数据的最初(或最顶层)的访问入口,代码如下:
Document doc = reader.read(new File(“Student.xml”));
4)使用Document对象的getRootElement方法获取要解析的XML文档的根元素,该方法返回值类型为Element。Element 对象表示 XML文档中的元素。元素可包含属性、其它元素或文本。如果元素含有文本,则在文本节点中表示该文本,代码如下:
Element root = doc.getRootElement();
5)接下来,可以使用Element对象提供的方法继续解析XML文档,例如:其elements方法用来获取当前元素下的所有子元素,代码如下:
List elements = root.elements();

package com.lyc.xml;

import java.util.ArrayList;
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;

//xml文件的内容读取
public class ReadXmlDemo {

public static void main(String[] args) {
	SAXReader reader = new SAXReader();
	try {
		ArrayList arrayList = new ArrayList();
		Document document = reader.read("student.xml");//文档对象
		Element root = document.getRootElement();//根元素
		List elements = root.elements();//获取跟元素下所有的子元素
		for (int i = 0; i < elements.size(); i++) {
			Student stu = new Student();
			Element ele = (Element) elements.get(i);//获取跟元素所有student对象
			Attribute attribute = ele.attribute(0);
			stu.setId(Integer.valueOf(attribute.getValue()));//xml中的属性id attribute.getValue()
			stu.setName(ele.elementText("name"));//elementText获取元素
			stu.setAge(Integer.valueOf(ele.elementText("age")));
			stu.setSex(ele.elementText("gender"));
			stu.setScore(Double.parseDouble(ele.elementText("score")));
			arrayList.add(stu);
		}
		
		for (Student student : arrayList) {
			System.out.println(student);
		}
	} catch (DocumentException e) {
		e.printStackTrace();
	}
}

}

package com.lyc.xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

//xml文件的生成(写)
public class StringOutputXml {

public static void main(String[] args) {
	List list = new ArrayList();
	list.add(new Student(1,"张飞","男",32,89));
	list.add(new Student(2,"张飞1","男",31,87));
	list.add(new Student(3,"张飞2","男",33,85));
	list.add(new Student(4,"张飞3","男",34,84));
	list.add(new Student(5,"张飞4","男",35,83));
	
	Document listdoc = DocumentHelper.createDocument();//创建document对象
	Element rootElement =  listdoc.addElement("students");//添加根元素
	
	for (Student student : list) {//添加子元素和文本内容
		Element stElement = rootElement.addElement("student");
		stElement.addAttribute("id",student.getId()+"");
		stElement.addElement("name").addText(student.getName());
		stElement.addElement("age").addText(student.getAge()+"");
		stElement.addElement("gender").addText(student.getSex());
		stElement.addElement("score").addText(student.getScore()+"");
	}
	
	
	try {
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("student1.xml"),"utf-8");
		//创建一个漂亮的输出格式对象
		OutputFormat opf = OutputFormat.createPrettyPrint();
		
		//创建写xml文档对象
		XMLWriter writer = new XMLWriter(osw,opf);
		writer.write(listdoc);//将上面创建的文档对象通过XMLWriter写出,写出到student2.xml中
		osw.close();
		writer.close();
		System.out.println("完成");
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}

}

package com.lyc.xml;

/**

  • model
  • 学生类
  • @author JLB

*/
public class Student {

private int id;
private String name;
private String sex;
private int age;
private double score;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public double getScore() {
	return score;
}
public void setScore(double score) {
	this.score = score;
}
@Override
public String toString() {
	return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", score=" + score + "]";
}
public Student() {
	super();
}
public Student(int id, String name, String sex, int age, double score) {
	super();
	this.id = id;
	this.name = name;
	this.sex = sex;
	this.age = age;
	this.score = score;
}

}

//操作的xml文件


	张飞
	17
	
	63



	赵云
	18
	
	89



	关羽
	17
	
	78



	韩愈
	16
	
	89



	程咬金
	17
	
	68



	李开
	17
	
	88

你可能感兴趣的:(学习)