解析XML之JDOM

简述

JDOM是一个开源项目,它基于树型结构,
	利用纯JAVA的技术对XML文档实现解析,生成,序列化以及多种操作(http://jdom.org)
JDOM直接为JAVA编程服务,它利用更为强有力的JAVA语言的诸多特性(方法重载,集合概念等),
	把SAX和DOM的功能有效地结合起来
JDOM是用Java语言读,写,操作XML的新API函数,
	在直接,简单和高效的前提下,这些API函数被最大限度的优化
JDOM架包下载(包含架包,源码,API,测试Junit,定位某个节点或者属性,直接取得它的xpath)
	http://www.jdom.org/dist/binary/

创建XML文件

<?xml version="1.0" encoding="UTF-8"?>
<students>
	<!--This is my comments-->
	<student no="1001">
		<name>lwc</name>
		<sex>boy</sex>
		<age>20</age>
	</student>
</students>

案例

package com.itlwc;

import java.io.FileWriter;

import org.jdom2.Comment;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class TestJdom {
	public static void main(String[] args) {
		try {
			Document document = new Document();
			//创建根元素
			Element elementStudents = new Element("students");
			document.addContent(elementStudents);
			//创建注释
			Comment comment = new Comment("This is my comments");
			elementStudents.addContent(comment);
			//创建子元素student
			Element elementStudent = new Element("student");
			elementStudent.setAttribute("no", "1001");
			elementStudents.addContent(elementStudent);
			//创建子元素student的子元素name,sex,age
			Element elementName = new Element("name");
			elementName.setText("lwc");
			Element elementSex = new Element("sex");
			elementSex.setText("boy");
			Element elementAge = new Element("age");
			elementAge.setText("20");
			elementStudent.addContent(elementName);
			elementStudent.addContent(elementSex);
			elementStudent.addContent(elementAge);
			Format format = Format.getPrettyFormat();
			format.setIndent("    ");
			XMLOutputter out = new XMLOutputter(format);
			out.output(document, new FileWriter("jdom.xml"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

JDOM遍历XML

package com.itlwc;

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

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class TestJdom {
	public static void main(String[] args) {
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(new File("jdom.xml"));
			// 获取根元素
			Element element = document.getRootElement();
			System.out.println(element.getName());
			// 获取根元素的子元素student
			Element elementStudent = element.getChild("student");
			System.out.println(elementStudent.getName());
			// 遍历子student的子元素
			List list = elementStudent.getChildren();
			for (int i = 0; i < list.size(); i++) {
				Element elementSon = (Element) list.get(i);
				String elementSonName = elementSon.getName();
				String elementSonText = elementSon.getText();
				System.out.println(elementSonName + ":" + elementSonText);
			}
			XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()
					.setIndent("    "));
			out.output(document, new FileOutputStream("jdom2.xml"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
/*
打印结果:
	students
	student
	name:lwc
	sex:boy
	age:20
*/

你可能感兴趣的:(解析XML之JDOM)