-
package com.common.xml;
-
-
import java.lang.reflect.Field;
-
-
//将java对象,格式化为xml格式输出,方便监控java对象的相关参数
-
//author:程晓鹏
-
//date:2016.05.19
-
public class Java2Xml {
-
-
private String[] javaNormalType = {"byte", "shot", "int", "long", "float", "double", "char", "boolean", "String", "Object"}; //java基本数据类型
-
public enum DisplayType {Normal, Lower, Upper}; //xml节点显示类型
-
private String[] filterTypeCollection = null;
-
private String[] filterClassCollection = null;
-
private StringBuilder result = null;
-
private String strOffset = ""; //偏移量填充字符
-
private DisplayType _type;
-
-
//默认构造函数
-
public Java2Xml(){
-
strOffset = "\t";
-
_type = DisplayType.Normal;
-
result = new StringBuilder();
-
-
}
-
-
//加载java对象
-
public void loadJavaObject(Object data){
-
result.setLength(0);
-
this.build(data, 0, "");
-
}
-
-
//获取xml结果值
-
public String getXmlResult(){
-
return result.toString();
-
}
-
-
//获取xml结果值
-
public String getXmlResult0(){
-
String result = this.getXmlResult();
-
return result.substring(0, result.length()-1);
-
}
-
-
//构建数组集合对象
-
private void buildListType1(Object data, int offset, String className){
-
++offset;
-
String strClassName = getClassName(className, ";");
-
getLogInfo(getXmlItem(strClassName), offset); //添加List节点
-
buildListData(data, offset);
-
getLogInfo(getXmlItemEnd(strClassName), offset);
-
--offset;
-
}
-
-
//构建List对象集合类型
-
private void buildListType2(Object data, int offset, String className){
-
if(data != null){
-
++offset;
-
String strClassName = getClassName(className, ">"); //获取真实的类名
-
getLogInfo(getXmlItem(strClassName), offset); //添加List节点
-
Field[] field = data.getClass().getDeclaredFields();
-
for(int i=0; i<field.length; i++){
-
String strClassName2 = getClassName(field[i].getGenericType().toString(),";");
-
if("object".equals(strClassName2.toLowerCase())){
-
try {
-
field[i].setAccessible(true); //设置可访问数据
-
buildListData(field[i].get(data), offset); //构建list集合数据
-
} catch (IllegalArgumentException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (IllegalAccessException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
-
getLogInfo(getXmlItemEnd(strClassName), offset);
-
}
-
}
-
-
//构建有java基本数据类型,构建的泛型集合对象 程晓鹏 2016.05.25 add
-
private void buildListType3(Object data, int offset, String className, String memeberName){
-
if(data != null){
-
String strClassName = getClassName(className, ">"); //获取真实的类名
-
++offset;
-
getLogInfo(getXmlItem(memeberName), offset); //添加List节点,采用成员名称,作为节点名称
-
Field[] field = data.getClass().getDeclaredFields();
-
for(int i=0; i<field.length; i++){
-
String strClassName2 = getClassName(field[i].getGenericType().toString(),";");
-
if("object".equals(strClassName2.toLowerCase())){
-
try {
-
field[i].setAccessible(true); //设置可访问数据
-
buildListData2(field[i].get(data), offset, strClassName); //构建list集合数据
-
} catch (IllegalArgumentException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (IllegalAccessException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
-
getLogInfo(getXmlItemEnd(memeberName), offset);
-
-
}
-
}
-
-
//构建由java基础类型,创建的数组类型
-
private void buildListType4(Object data, int offset, String className, String memeberName){
-
if(data != null){
-
++offset;
-
String strClassName = getClassName(className, ";"); //获取真实的类名
-
getLogInfo(getXmlItem(memeberName), offset); //添加List节点,采用成员变量名称
-
Object len = java.lang.reflect.Array.getLength(data);
-
int iLen = Integer.parseInt(len.toString());
-
for(int i=0; i<iLen; i++){
-
Object arrayObj = java.lang.reflect.Array.get(data, i);
-
if(arrayObj != null){
-
++offset;
-
getLogInfo(getXmlItem(strClassName, arrayObj.toString()),offset); //添加叶子节点
-
--offset;
-
}
-
}
-
getLogInfo(getXmlItemEnd(memeberName), offset);
-
}
-
}
-
-
//构建集合数据
-
private void buildListData(Object data, int offset){
-
if(data != null){
-
Object len = java.lang.reflect.Array.getLength(data);
-
int iLen = Integer.parseInt(len.toString());
-
for (int j=0; j<iLen; j++){
-
Object arrayObj = java.lang.reflect.Array.get(data, j);
-
if(arrayObj != null){
-
this.build(arrayObj, ++offset, "item"); //进行递归调用
-
--offset;
-
}
-
}
-
}
-
}
-
-
//构建由java基础类型,创建的泛型集合成员 程晓鹏2016.05.25 add
-
private void buildListData2(Object data, int offset, String className){
-
if(data != null){
-
Object len = java.lang.reflect.Array.getLength(data);
-
int iLen = Integer.parseInt(len.toString());
-
for (int j=0; j<iLen; j++){
-
Object arrayObj = java.lang.reflect.Array.get(data, j);
-
if(arrayObj != null){
-
++offset;
-
getLogInfo(getXmlItem(className, arrayObj.toString()),offset); //添加叶子节点
-
--offset;
-
}
-
}
-
}
-
}
-
-
//构建叶子节点
-
private void buildLeafNode(String memberName, Object data, int offset){
-
String memberValue = "null"; //默认值为null
-
if(data != null){
-
memberValue =data.toString();
-
}
-
++offset;
-
getLogInfo(getXmlItem(memberName, memberValue),offset); //添加叶子节点
-
--offset;
-
}
-
-
//构建单List泛型集合对象 程晓鹏 2017.03.03 add
-
private void buildListData2_2(Object data, int offset, String className){
-
if(data != null){
-
Object len = java.lang.reflect.Array.getLength(data);
-
int iLen = Integer.parseInt(len.toString());
-
for (int j=0; j<iLen; j++){
-
Object arrayObj = java.lang.reflect.Array.get(data, j);
-
if(arrayObj != null){
-
++offset;
-
this.build(arrayObj, offset, "");
-
--offset;
-
}
-
}
-
}
-
}
-
-
//支撑单List泛型集合对象 程晓鹏 2017.03.03 add
-
private void buildSingleList(Object obj, int offset, String xmlNode){
-
try {
-
getLogInfo(getXmlItem(xmlNode),offset); //添加开始节点
-
Field[] field = obj.getClass().getDeclaredFields();
-
Field myfield = field[1];
-
myfield.setAccessible(true); //设置可访问数据
-
Object membObj = myfield.get(obj);
-
buildListData2_2(membObj, offset, xmlNode); //采用新的构建方法 程晓鹏 2017.03.03 add
-
getLogInfo(getXmlItemEnd(xmlNode),offset); //添加结束节点
-
return;
-
} catch (IllegalArgumentException e) {
-
e.printStackTrace();
-
} catch (IllegalAccessException e) {
-
e.printStackTrace();
-
} //获取私有成员数据
-
}
-
-
//进行构建
-
private void build(Object data, int offset, String xmlNode){
-
Field[] field = data.getClass().getDeclaredFields();
-
String strRootClass = getClassName(data.getClass().getName());
-
String strXmlNode = "".equals(xmlNode)?strRootClass:xmlNode; //节点名称
-
int iDataType = validDataType(strXmlNode, "");
-
if(iDataType == 6){
-
this.buildSingleList(data, offset, strXmlNode); //支撑单List泛型集合对象 程晓鹏 2017.03.03 add
-
return;
-
}else if (iDataType == 7){
-
String className = strXmlNode.substring(0, strXmlNode.length()-1); //去掉最后一个';'字符
-
this.buildListType1(data, --offset, className); //支撑单List泛型集合对象 程晓鹏 2017.03.03 add
-
return;
-
}else{
-
;
-
}
-
-
getLogInfo(getXmlItem(strXmlNode),offset); //添加开始节点
-
for(int i=0; i<field.length; i++){
-
String strMemberName = field[i].getName(); //成员变量名称
-
String strSubClassName = field[i].getGenericType().toString();
-
iDataType = validDataType(strSubClassName, strMemberName); //验证数据类型
-
try {
-
field[i].setAccessible(true); //设置可访问数据
-
Object membObj = field[i].get(data); //获取私有成员数据
-
//field[i].getModifiers() Java语言修饰符返回整数编码。修饰符包括public, protected, private, final, static, abstract 和interface及Java虚拟机的常数
-
//field[i].getModifiers()=10;为静态成员
-
if(10==field[i].getModifiers() && 2!=iDataType){ //对类中的静态成员进行过滤掉
-
continue;
-
}else if(membObj==null && 2!=iDataType){ //当节点为null时,跳过
-
continue;
-
}else if (iDataType < 0){ //过滤的类型,直接跳过
-
continue;
-
}else if (1 == iDataType){ //构建对象集合类型
-
buildListType1(membObj, offset, strSubClassName);
-
}else if (3 == iDataType){ //构建List对象集合类型
-
buildListType2(membObj, offset, strSubClassName);
-
}else if (4 == iDataType){ //构建java基础类型,创建的List对象集合
-
buildListType3(membObj, offset, strSubClassName, strMemberName);
-
}else if (5 == iDataType){ //构建java基础类型,创建的数组类型
-
buildListType4(membObj, offset, strSubClassName, strMemberName);;
-
}else if (2 == iDataType){
-
buildLeafNode(strMemberName, membObj, offset); //构建叶子节点
-
}else{
-
this.build(membObj, ++offset, ""); //进行递归调用
-
--offset; //递归完成处理后,将偏移量--
-
}
-
}catch (IllegalArgumentException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (IllegalAccessException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
getLogInfo(getXmlItemEnd(strXmlNode),offset); //添加结束节点
-
}
-
-
//验证数据类型
-
//clasName:类名称(包含包);memberName:成员变量名称
-
//0:默认值; 1:是集合成员; -1:是过滤成员;2:java关键字
-
private int validDataType(String className, String memberName){
-
int result = 0;
-
String strPackage = getGenericType(className); //包名称
-
String strCalssName = getClassName(strPackage); //真实的类名
-
if(validFilterMemberKey(memberName)){ //过滤的数据类型
-
result = -1;
-
}else if(validFilterClassKey(strPackage)){ //过滤的类名称
-
result = -2;
-
}else if(className.indexOf("[L")>0){ //判断是否是数组集合成员
-
strCalssName = getClassName(strPackage, ";"); //重新获取,真实的类名
-
if(validJavaDataTypeKey(strCalssName)){
-
result = 5; //java基础类型,创建的数组类型
-
}else{
-
result = 1; //类结构的数组
-
}
-
}else if(className.indexOf("List<")>0){ //判断是否为List集合成员
-
strCalssName = getClassName(strPackage, ">"); //重新获取,真实的类名
-
if(validJavaDataTypeKey(strCalssName)){
-
result = 4; //类型为java关键字的泛型集合
-
}else{
-
result = 3; //为泛型集合类型
-
}
-
}else if(validJavaDataTypeKey(strCalssName)){ //java关键字
-
result = 2;
-
}else if(strCalssName.equals("ArrayList")){
-
result = 6; //为独立的List对象 程晓鹏 2017.03.03 add
-
}else if (className.indexOf(";")>0){
-
result = 7; //为独立的数组对象 程晓鹏 2017.03.03 add
-
}
-
else{
-
;
-
}
-
return result;
-
}
-
-
//获取xml节点项
-
private String getXmlItem(String xmlNode, String text){
-
StringBuilder sb = new StringBuilder();
-
sb.append(getXmlItem(xmlNode));
-
sb.append(text);
-
sb.append(getXmlItemEnd(xmlNode));
-
-
return sb.toString();
-
}
-
-
//获取xml开始节点
-
private String getXmlItem(String xmlNode){
-
StringBuilder sb = new StringBuilder();
-
sb.append("<");
-
sb.append(getXmlNode(xmlNode));
-
sb.append(">");
-
-
return sb.toString();
-
}
-
-
//获取xml结束节点
-
private String getXmlItemEnd(String xmlNode){
-
StringBuilder sb = new StringBuilder();
-
sb.append("");
-
sb.append(getXmlNode(xmlNode));
-
sb.append(">");
-
-
return sb.toString();
-
}
-
-
private String getXmlNode(String xmlNode){
-
String result = "";
-
switch(this._type){
-
case Normal:
-
result = xmlNode;
-
break;
-
case Upper:
-
result =xmlNode.toUpperCase();
-
break;
-
case Lower:
-
result = xmlNode.toLowerCase();
-
break;
-
default:
-
result = xmlNode;
-
break;
-
}
-
-
return result;
-
}
-
-
//获取日志信息
-
private void getLogInfo(String data, int offset){
-
StringBuilder strXmlItem = new StringBuilder();
-
strXmlItem.append(printOffsetStr(offset));
-
strXmlItem.append(data);
-
strXmlItem.append("\n");
-
-
//System.out.print(strXmlItem.toString()); //打印
-
result.append(strXmlItem);
-
}
-
-
//打印偏移数据量
-
private String printOffsetStr(int size){
-
StringBuilder result = new StringBuilder();
-
for(int i=0; i<size; i++){
-
result.append(this.strOffset);
-
}
-
-
return result.toString();
-
}
-
-
//设置过滤成员集合数据,打印参数不打印该数据
-
public void setFilterMemberCollection(String[] data){
-
this.filterTypeCollection = data;
-
}
-
-
//设置过滤的类集合数据,打印参数不打印该数据
-
public void setFilterClassCollcetin(String[] data){
-
this.filterClassCollection = data;
-
}
-
-
//设置偏移字符串
-
public void setOffsetString(String offsetStr){
-
this.strOffset = offsetStr;
-
}
-
-
public void setXmlNodeType(DisplayType type){
-
this._type = type;
-
}
-
-
//验证java数据类型是否为java基本数据类型
-
private boolean validJavaDataTypeKey(String key){
-
boolean result = false;
-
for(int i=0; i<javaNormalType.length; i++){
-
if (key.toLowerCase().equals(javaNormalType[i].toLowerCase())){
-
result = true;
-
break;
-
}
-
}
-
return result;
-
}
-
-
//验证过过滤成员
-
private boolean validFilterMemberKey(String key){
-
boolean result = false;
-
if(filterTypeCollection != null && filterTypeCollection.length > 0){
-
for(int i=0; i<filterTypeCollection.length; i++){
-
if (key.toLowerCase().equals(filterTypeCollection[i].toLowerCase())){
-
result = true;
-
break;
-
}
-
}
-
}
-
return result;
-
}
-
-
//验证过滤的信息
-
private boolean validFilterClassKey(String key){
-
boolean result = false;
-
if(this.filterClassCollection != null && filterClassCollection.length > 0){
-
for(int i=0; i<filterClassCollection.length; i++){
-
if (key.toLowerCase().equals(filterClassCollection[i].toLowerCase())){
-
result = true;
-
break;
-
}
-
}
-
}
-
return result;
-
}
-
-
//获取类型,去除前面的"class "字符串
-
private String getGenericType(String data){
-
String result = "";
-
if(data.length() > 6 && data.indexOf("class ")!=-1){
-
result = data.substring(6, data.length());
-
}else{
-
result = data;
-
}
-
return result;
-
}
-
-
//获取类名称
-
private String getClassName(String data){
-
String result = "";
-
int idx = data.lastIndexOf("."); //类名全路径最后一个.的位置,为获取类名称做准备
-
if (idx != -1){
-
++idx;
-
result = data.substring(idx, data.length());
-
}else{
-
result = data;
-
}
-
return result;
-
}
-
-
//获取类名2
-
private String getClassName(String data, String endChar){
-
String result = "";
-
int idx = data.lastIndexOf("."); //类名全路径最后一个.的位置,为获取类名称做准备
-
int idx2 = data.lastIndexOf(endChar);
-
if (idx != -1 && idx2 != -1){
-
++idx;
-
result = data.substring(idx, idx2);
-
}else if(idx != -1 && idx2<0){
-
++idx;
-
result = data.substring(idx, data.length());
-
}else{
-
result = data;
-
}
-
return result;
-
}
-
}
Java2Xml.rar
阅读(2464) | 评论(0) | 转发(0) |