xml文件的jdom解析

dom4j解析:   http://blog.csdn.net/java4found/article/details/8665982
dom解析: http://blog.csdn.net/java4found/article/details/8665034
sax解析: http://blog.csdn.net/java4found/article/details/8663214


jdom解析xml,是基于树型结构,其利用的是纯java技术,实现对xml文档的各种操作。jdom可以说是直接为java编程服务的,把sax与dom的功能有效的结合起来。
由于jdom致力于隐藏解析xml的复杂性,因此用jdom解析xml就和java new一个新的对象一般,操作也是非常容易的。在jdom中,xml本身就是一个Document实例,每一个元素标签则为Element实例,属性则为Attribute实例,通过这些对象,对xml的操作会显得格外的简单。
使用jdom要下载jdom的开发包:jdom-jar下载地址: http://www.jdom.org/dist/binary/
将jdom.jar加入到工程目录中。
下面是一个person.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person id="328">
		<name>zhangsan</name>
		<age>23</age>
	</person>
	<person id="303">
		<name>lisi</name>
		<age>22</age>
	</person>
</persons>

将解析出来的对象封装成Person对象,以下是Person类:
public class Person {
	private int id;
	private String name;
	private int age;

	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 int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

	public Person(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Person() {
		super();
	}
}

使用JDom来解析person.xml:
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class JDomTest {
	public static void main(String[] args) {
		//创建解析器对象
		SAXBuilder builder = new SAXBuilder();
		//获取xml输入流
		InputStream in = JDomTest.class.getClassLoader().getResourceAsStream(
				"person.xml");
		try {
			//获得代表整个xml的Document对象
			Document document = builder.build(in);
			//获取根结点<persons>
			Element root = document.getRootElement();
			//获取根结点下的子结点<person>集合
			List<Element> list = root.getChildren();
			
			ArrayList<Person> persons = new ArrayList<Person>();
			Person person = null;
			//遍历<person>对应数据内容,将数据封装成Person对象后,放入集合中
			for (Element e : list) {
				person = new Person();
				//获取当前<person>结点的属性对象,并得到id的值
				Attribute idAtt = e.getAttribute("id");
				person.setId(idAtt.getIntValue());
				//获取<person>的子结点集合
				List<Element> childlist = e.getChildren();
				//遍历集合,获取name与age的值
				for (Element ce : childlist) {
					if ("name".equals(ce.getName())) {
						String name = ce.getText();
						person.setName(name);
					}
					if ("age".equals(ce.getName())) {
						String age = ce.getText();
						person.setAge(Integer.valueOf(age));
					}
				}
				//完成一次<person>遍历后,将获取的Person对象存入集合中。
				persons.add(person);
				person = null;
			}
			//完成数据读取,打印调试。
			printList(persons);
		} catch (JDOMException | IOException e) {
			e.printStackTrace();
		}
	}
	//用于打印集合。
	public static void printList(ArrayList<Person> persons) {
		for (Person p : persons) {
			System.out.println(p);
		}
	}
}


你可能感兴趣的:(jdom,xml解析,jdom解析)