对象生成xml

你肯定想过,给你一个对象就生成对应的xml格式文件,这里就是一个。

CreateXMLUtils

package common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Text;

/**
* 生成XML的工具类
*@date Dec 23,200910:04:38 AM
*@author zhangjp
*@param
*/
public class CreateXMLUtil {

/**
* 对普通的list进行生成XML
* @param list
* @param UID 是否把serialVersionUID也些在XML中
* @returnCreateXMLUtil.java
*/
public static String getClassXML(List<?> list,boolean UID){
try {
if (list == null || list.size() == 0) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement("kedou");
for (int i = 0; i < list.size(); i++) {
String className = list.get(i).getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = list.get(i).getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(list.get(i))==null){
continue;
}
String objValue = fields[j].get(list.get(i)).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);

if("serialVersionUID".equals(fieldName)){
if(UID==false){
continue;
}
}

element.addAttribute(fieldName,objValue);

// HttpServletResponse response =
// ServletActionContext.getResponse();
// PrintWriter out = response.getWriter();
// response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control","no-cache");
// out.print(document.asXML());

}


}
return document.asXML();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}


/**
* 对普通的list进行生成XML 生成XMLrootNode为<kedou>
* @param list
* @returnCreateXMLUtil.java
*/
public static String getClassXML(List<?> list ){
try {
if (list == null || list.size() == 0) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement("kedou");
for (int i = 0; i < list.size(); i++) {
String className = list.get(i).getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = list.get(i).getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(list.get(i))==null){
continue;
}
String objValue = fields[j].get(list.get(i)).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);
if("serialVersionUID".equals(fieldName)){
continue;
}else{
element.addAttribute(fieldName,objValue);
}

// HttpServletResponse response =
// ServletActionContext.getResponse();
// PrintWriter out = response.getWriter();
// response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control","no-cache");
// out.print(document.asXML());

}


}
return document.asXML();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

/**
* 对普通的list进行生成XML
* @param list
* @param rootName 根节点名称
* @returnCreateXMLUtil.java
*/
public static String getClassXML(List<?> list,String rootName){
try {
if (list == null || list.size() == 0) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement(rootName);
for (int i = 0; i < list.size(); i++) {
String className = list.get(i).getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = list.get(i).getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(list.get(i))==null){
continue;
}
String objValue = fields[j].get(list.get(i)).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);
if("serialVersionUID".equals(fieldName)){
continue;
}else{
//以下为两种风格输出XML
//element.addAttribute(fieldName,objValue);
Element ele = element.addElement(fieldName);
ele.addText(objValue);

}

// HttpServletResponse response =
// ServletActionContext.getResponse();
// PrintWriter out = response.getWriter();
// response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control",String rootName,boolean UID){
try {
if (list == null || list.size() == 0) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement(rootName);
for (int i = 0; i < list.size(); i++) {
String className = list.get(i).getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = list.get(i).getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(list.get(i))==null){
continue;
}
String objValue = fields[j].get(list.get(i)).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);
if("serialVersionUID".equals(fieldName)){
if(UID==false){
continue;
}
}else{
element.addAttribute(fieldName,"no-cache");
// out.print(document.asXML());

}


}
return document.asXML();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

/**
* 对一般的pojo进行生成相对应得xml
* @param obj
* @param UID 是否把serialVersionUID也些在XML中
* @returnCreateXMLUtil.java
*/
public static String getClassXML(Object obj,boolean UID){
try {
if (obj == null) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement("kedou");
String className = obj.getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = obj.getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(obj)==null){
continue;
}
String objValue = fields[j].get(obj).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);
if("serialVersionUID".equals(fieldName)){
if(UID==false){
continue;
}
}
element.addAttribute(fieldName,objValue);

// HttpServletResponse response =
// ServletActionContext.getResponse();
// PrintWriter out = response.getWriter();
// response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control","no-cache");
// out.print(document.asXML());

}

return document.asXML();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}


/**
* 对一般的pojo进行生成相对应得xml
* @param obj
* @param rootName 根节点名称
* @param UID 是否把serialVersionUID也写在XML中
* @returnCreateXMLUtil.java
*/
public static String getClassXML(Object obj,boolean UID){
try {
if (obj == null) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement(rootName);
String className = obj.getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = obj.getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(obj)==null){
continue;
}
String objValue = fields[j].get(obj).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);
if("serialVersionUID".equals(fieldName)){
if(UID==false){
continue;
}
}
Element ele = element.addElement(fieldName);
ele.addText(objValue);
// HttpServletResponse response =
// ServletActionContext.getResponse();
// PrintWriter out = response.getWriter();
// response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control","no-cache");
// out.print(document.asXML());

}

return document.asXML();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}
public static String getClassXML(Object obj){
try {
if (obj == null) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement("kedou");
String className = obj.getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = obj.getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(obj)==null){
continue;
}
String objValue = fields[j].get(obj).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);
element.addAttribute(fieldName,"no-cache");
// out.print(document.asXML());

}

return document.asXML();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

/**
*
*
@author: zhangjp
@功能:把一个简单和合一个list对象生成的xml合并成一个xml
@公用: yes
@date: Feb 14,2010
*/
public static String getClassXML(Object object,String objRootName,List list,String listRootName){
try {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element objectNode = CreateXMLUtil.getClassXMLDocument(object,objRootName,false);
Element listNode = CreateXMLUtil.getClassXMLDocument(list,listRootName);
root.add(objectNode);
root.add(listNode);
return document.asXML();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/**
*
@author: zhangjp
@功能:获得object对象的根元素
@公用:是
@date: Feb 14,2010
*/
public static Element getClassXMLDocument(Object obj,boolean UID){
try {
if (obj == null) {
return null;
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement(rootName);
String className = obj.getClass().getName();
String name = className.substring(className.lastIndexOf('.')+1);
Element element = root.addElement(name);
Field[] fields = obj.getClass().getDeclaredFields();

for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
if(fields[j].get(obj)==null){
continue;
}
String objValue = fields[j].get(obj).toString();
String fieldName = fields[j].toString().substring(fields[j].toString().lastIndexOf('.')+1);
if("serialVersionUID".equals(fieldName)){
if(UID==false){
continue;
}
}
Element ele = element.addElement(fieldName);
ele.addText(objValue);

// HttpServletResponse response =
// ServletActionContext.getResponse();
// PrintWriter out = response.getWriter();
// response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control","no-cache");
// out.print(document.asXML());

}

return root;
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

/**
*
@author: zhangjp
@功能:获得list对象的根元素
@公用: 是
@date: Feb 14,2010
*/
public static Element getClassXMLDocument(List<?> list,"no-cache");
// out.print(document.asXML());

}


}
return root;
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

public void writeDocument(String str,File file) throws Exception{ try { /* StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xmlwriter = new XMLWriter(writer,format); xmlwriter.write(str); String outString = writer.toString(); */ OutputStream os = new FileOutputStream(file); PrintWriter out = new PrintWriter(new OutputStreamWriter(os,"utf-8")); System.out.println(str); out.print(str); out.flush(); } catch (Exception ex) { ex.printStackTrace(); } }/** * @author: zhangjp @功能:test @公用: @date: Feb 14,2010 */public static void main(String[] args) { /*** * test here */}}

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念