jdom入门

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());
			}
		}
	}
}	



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值