XML转实体类工具

版权声明:内容记录学习过成文章,仅供参考 https://blog.csdn.net/qq_40195958/article/details/86518752
这里使用到了JOX依赖
<!-- https://mvnrepository.com/artifact/com.wutka/jox -->
<dependency>
    <groupId>com.wutka</groupId>
    <artifactId>jox</artifactId>
    <version>1.16</version>
</dependency>

代码

/**
 * 工具类
 *@Project:gsj_qcdzh	
 *@ClassName:BeanXmlMapping
 *@Company: JIT Northeast R & D
 * @Description:TODO
 * @author dongwei
 * @Date 2019年1月17日 上午9:47:33
 * @Version V1.0
 */
public class BeanXmlMapping {

	public static Object fromXML(InputStream xml, Class className)
			throws Exception {
		// ByteArrayInputStream xmlData = new ByteArrayInputStream(xml);
		JOXBeanInputStream joxIn = new JOXBeanInputStream(xml);
		try {
			return (Object) joxIn.readObject(className);
		} catch (Exception exc) {
			throw exc;
		} finally {
			try {
				xml.close();
				joxIn.close();
			} catch (Exception e) {
				throw e;
			}
		}
	}
}
// 应用
	public static Archives searchDZDAxmldata() throws Exception{
		//通过路径读取相应xml配置文件
	    String path = "/temp/xml/test.xml";
	    path = DzdaGenerateUtil.class.getResource(path).getFile();
//  获得文件流	    
		FileInputStream fis = new FileInputStream(path);
//  调用实体类进行转换		
		Archives ai = (Archives)BeanXmlMapping.fromXML(fis, Archives.class);
		
		return ai;
	}

部分实体类与xml文件数据

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

一个标签生成一个对应实体类组成的数组

猜你喜欢

转载自blog.csdn.net/qq_40195958/article/details/86518752
今日推荐