当前位置: 首页 > 工具软件 > Betwixt > 使用案例 >

java与commons_使用apache.commons.betwixt实现XML与Java对象互转

督俊雅
2023-12-01

##关于betwixt

##如何引用

在maven项目中添加以下依赖

commons-betwixt

commons-betwixt

0.8

##注意事项 转化过程对 大小写是敏感的,

例如 XML中的结点名称是大写的,而Java类中的成员变量是小写的。那么转换将不会成功,得到的结点值是空的。

##核心代码

###创建一个测试对象

package com.icim.entity;

public class TestObj {

private String name;

private Integer age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

/**

* 必须有一个空的构造方法,否则会报错

*/

public TestObj() {

}

public TestObj(String name, Integer age) {

super();

this.name = name;

this.age = age;

}

@Override

public String toString() {

return "TestObj [name=" + name + ", age=" + age + "]";

}

}

###转换对象

package com.icim.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.StringReader;

import java.io.StringWriter;

import java.io.UnsupportedEncodingException;

import org.apache.commons.betwixt.io.BeanReader;

import org.apache.commons.betwixt.io.BeanWriter;

import org.apache.log4j.Logger;

import com.icim.entity.TestObj;

public class XmlParseUtil {

private static final Logger logger = Logger.getLogger(XmlParseUtil.class);

private static final String XML_ROOT_NODE_NAME = "TRANSACTION";

public static final String XML_HEADER = "<?xml version='1.0' encoding='UTF-8' ?>";

/**

* 将XML字符串 转换成 对象

* @param strInMsg : XML内容

* @param clazz

* @return

*/

public static Object inStr2Obj(String strInMsg, @SuppressWarnings("rawtypes") Class clazz) {

BeanReader beanReader = new BeanReader();

Object parse = null;

StringReader xmlReader = new StringReader(strInMsg);

beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);

beanReader.getBindingConfiguration().setMapIDs(false);

try {

beanReader.registerBeanClass(XML_ROOT_NODE_NAME, clazz);

parse = beanReader.parse(xmlReader);

} catch (Exception ex) {

logger.error(ex.toString());

}

return parse;

}

/**

* 将 对象 转换成 XML字符串

* @param inObj

* @return

*/

public static String obj2Str(Object inObj) {

StringWriter sw = new StringWriter();

BeanWriter beanWriter = new BeanWriter(sw);

sw.write(XML_HEADER);

try {

beanWriter.setEndOfLine("");

beanWriter.getBindingConfiguration().setMapIDs(false);

beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);

beanWriter.write(XML_ROOT_NODE_NAME, inObj);

} catch (Exception e) {

logger.error(e.toString());

}

return sw.toString();

}

/**

* 将XML文件转换成 对象

* @param fileName

* @param clazz

* @return

*/

public static Object file2Object(String fileName, Class clazz) {

String fileContent = file2String(fileName);

return inStr2Obj(fileContent, clazz);

}

/**

* 将XML文件转换成 对象

* @param fileName

* @param clazz

* @return

*/

public static Object file2Object(File file, Class clazz) {

String fileContent = file2tring(file);

return inStr2Obj(fileContent, clazz);

}

/**

* 读取文件全部内容

* @param fileName

* @return

*/

private static String file2String(String fileName) {

File file = new File(fileName);

return file2tring(file);

}

/**

* 读取文件全部内容

* @param file

* @return

*/

private static String file2tring(File file) {

String encoding = "ISO-8859-1";

Long filelength = file.length();

byte[] filecontent = new byte[filelength.intValue()];

try {

FileInputStream in = new FileInputStream(file);

in.read(filecontent);

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

try {

return new String(filecontent, encoding);

} catch (UnsupportedEncodingException e) {

System.err.println("The OS does not support " + encoding);

e.printStackTrace();

return null;

}

}

//测试方法

public static void main(String[] args) {

TestObj obj = new TestObj("xiaoming", 18);

String xmlString = XmlParseUtil.obj2Str(obj);

System.out.println(xmlString);

TestObj obj2 = (TestObj) XmlParseUtil.inStr2Obj(xmlString, TestObj.class);

System.out.println(obj2);

}

}

输出结果

<?xml version='1.0' encoding='UTF-8' ?>18xiaoming

TestObj [name=xiaoming, age=18]

 类似资料: