本文主要介绍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>