dom4j创建xml文件

本文主要介绍dom4j创建xml文件,其中通过java反射技术获取指定方法返回值泛型类型,通过此类型获取其类的对应字段

首先,book文件

package com;

public class Book {
	private int id;
	private String name;
	
	public Book() {
		super();
	}
	
	public Book(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

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

然后,BookList.java文件

package com;

import java.util.ArrayList;
import java.util.List;

public class BookList {
	public List<Book> getList(){
		List<Book> list=new ArrayList<Book>();
		list.add(new Book(1,"Think in Java"));
		list.add(new Book(2,"CSS"));
		list.add(new Book(3,"JavaScript"));
		list.add(new Book(4,"HTML5"));
		return list;
	} 
}

最后,

package com;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
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;

public class TestXml {
	public static void main(String[] args) {
		Document document = DocumentHelper.createDocument();
		OutputFormat format = OutputFormat.createPrettyPrint();
		Class<?> clazz =null;
		try {
			clazz=Class.forName("com.BookList");
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		Method[] methods=clazz.getMethods();
		List<Book> bookList=null;
		Type type[]=null;
		try {
			bookList = (List<Book>) methods[0].invoke(new BookList());
			type=((ParameterizedType)methods[0].getGenericReturnType()).getActualTypeArguments();//获取<span style="white-space:pre">																		</span>指定方法返回值泛型类型
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
			e1.printStackTrace();
		}
		int len=type[0].toString().length();
		String c=type[0].toString().substring(6,len);
		Class<?> c1=null;
		try {
			c1=Class.forName(c);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		Field[] fields=c1.getDeclaredFields();
		format.setEncoding("UTF-8");
		Element booksElement = document.addElement("books");
		booksElement.addComment("this is books element");
		for (int i = 0; i < bookList.size(); i++) {
			Element bookElement = booksElement.addElement("book");
			//bookElement.addAttribute("show", "yes");
			Element idElement = bookElement.addElement(fields[0].getName());
			idElement.setText(String.valueOf(((Book)(bookList.get(i))).getId()));
			Element nameElement=bookElement.addElement(fields[1].getName());
			nameElement.setText(bookList.get(i).getName());
		}
		try {
			XMLWriter writer = new XMLWriter(
					new FileWriter(new File("xmlfile.xml")), format);
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

最后,程序生成的xmlfile.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<books>
  <!--this is books element-->
  <book>
    <id>1</id>
    <name>Think in Java</name>
  </book>
  <book>
    <id>2</id>
    <name>CSS</name>
  </book>
  <book>
    <id>3</id>
    <name>JavaScript</name>
  </book>
  <book>
    <id>4</id>
    <name>HTML5</name>
  </book>
</books>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值