当前位置: 首页 > 工具软件 > JDOM > 使用案例 >

jdom入门

权兴为
2023-12-01

1.关于jdom的介绍,参考官网(http://www.jdom.org/).下载导入到工程中即可是使用,下面通过两个实例简单介绍。

    应该说使用jdom没有什么难点,主要还是熟悉接口文档。官网有详细的文档,这里提供一个chm格式的下载地址

a)使用jdom写一个xml文件

public class Test1 {
	public static void main(String[] args) throws FileNotFoundException,
			IOException {

		Document doucument = new Document();
		doucument.detachRootElement();//
		Element rootElement = new Element("学生列表");
		doucument.setRootElement(rootElement);//设置根元素

		Element studentElement = new Element("学生").setAttribute("年级", "一年级");
		Element studentNameElement = new Element("姓名").setText("Ebuair");
		Element studentGenderElement = new Element("性别").setText("男");
		Element studentAgeElement = new Element("年龄").setText("20");
		
		Comment comment = new Comment("学生列表说明");
		rootElement.addContent(comment);//添加注释
		
		rootElement
				.addContent((studentElement).addContent(studentNameElement)
						                    .addContent(studentGenderElement)
						                    .addContent(studentAgeElement));// 方法链编程风格

		Format format = Format.getPrettyFormat();
		format.setIndent("    ");// 设置缩进格式为四个空格
		XMLOutputter outputter = new XMLOutputter(format);
		outputter.output(doucument, new FileOutputStream("studentlist.xml"));
	}
}

b)使用jdom读取xml文件,将读取的数据打印出来。

package com.ebuair.jdom;

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

import org.jdom2.Attribute;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;




import org.jdom2.util.IteratorIterable;

import com.ebuair.xml.Student;

public class Test2 {
	public static void main(String[] args) throws JDOMException, IOException {
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build("doc/student.xml");//获取xml文件
		
		Element rootElement = document.getRootElement();//根元素
		System.out.print(rootElement.getName() + "   ");
		
		List<Attribute> rootElementAttributes = rootElement.getAttributes();//根元素的属性
		for(Iterator<Attribute> iterator = rootElementAttributes.iterator(); iterator.hasNext();){
			Attribute attribute = iterator.next();
			String attributeName = attribute.getName();
			String attributeValue = attribute.getValue();
			System.out.println(attributeName + " :" + attributeValue);
		}
		List<Element> children = rootElement.getChildren();//所有子元素
		for(Element child : children){
			System.out.print(child.getName() + "   ");
			List<Attribute> attributes = child.getAttributes();
			Element nameElement = child.getChild("姓名");
			Element genderElement = child.getChild("性别");
			Element ageElement = child.getChild("年龄");
			for(Iterator<Attribute> iterator = attributes.iterator(); iterator.hasNext();){
				Attribute attribute = iterator.next();
				String attributeName = attribute.getName();
				String attributeValue = attribute.getValue();
				System.out.println(attributeName + " :" + attributeValue);
				System.out.println(nameElement.getName() + ":" + nameElement.getValue());
				System.out.println(genderElement.getName() + ":" + genderElement.getValue());
				System.out.println(ageElement.getName() + ":" + ageElement.getValue());
			}
		}
	}
}	



 类似资料: