您的当前位置:首页Dom4J解析xml文件动态转换为ListBean或者Map集合

Dom4J解析xml文件动态转换为ListBean或者Map集合

2020-08-20 来源:乌哈旅游
Dom4J解析xml⽂件动态转换为ListBean或者Map集合

⼤家在解析⼤量相似xml⽂件的时候是否会遇到这样⼀个问题:冗余的代码去set定义的实体对象Bean的值,基本都是⼀样的操作

⽽且毫⽆任何代码价值可⾔所以在这写了⼀个简单的例⼦,类封装了⼏个⽅法你只需传⼊xml⽂件,或者URL路径。和Bean.Class 类名

即可⾃动传回你需要的List集合。

需要解析的xml⽂件

0 2

0

张三0 qsina0@sina.com 1 1

1

192.168.1.0

2012-06-07 15:35:33

1

张三1 qsina1@sina.com 1 1

1

192.168.1.1

2012-06-07 15:35:33

在进⾏相关操作中需要如下包:

下⾯是贴出的代码:

package com.jeecms.common.util;

import java.lang.reflect.Field;import java.net.URL;

import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;import org.dom4j.Element;

import org.dom4j.io.SAXReader;

public class XmlParseUtil { /**

* 反射设置实体不同类型字段的值 <暂时只⽀持 ⽇期 字符串 boolean Integer值设置 待扩建> *

* @param field * @param obj * @param value

* @throws Exception */

public static void convertValue(Field field, Object obj, String value)

throws Exception {

SimpleDateFormat sim = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\"); if (field.getGenericType().toString().equals(\"class java.lang.Integer\")) { field.set(obj, Integer.parseInt(value));

} else if (field.getGenericType().toString().equals(\"boolean\")) { field.set(obj, Boolean.parseBoolean(value)); } else if (field.getGenericType().toString().equals( \"class java.util.Date\")) {

field.set(obj, sim.parse(value)); } else {

field.set(obj, value); } } /**

* 解析xml⽂件返回保存cls的List集合,如果返回码resultCode=1时则返回List为null *

* @param xml * @param cls * @return

* @throws Exception */

@SuppressWarnings(\"unchecked\")

public static List parseXml2List(String xml, Class cls) throws Exception { List lists = null;

Document doc = DocumentHelper.parseText(xml); Element et = doc.getRootElement(); String root = et.getName(); // 查看返回码是否为真.

List list = doc.selectNodes(\"//\" + root + \"/resultCode\"); if (!list.isEmpty() && list.size() > 0) { Element element = list.get(0);

String returnResult = element.getText(); if (returnResult.equals(\"0\")) {

List father = doc.selectNodes(\"//\" + root + \"/\"+ cls.getSimpleName()+\"s\"); //判断对象⽗节点是否有包含数据

if(father!=null&&!father.isEmpty()&&father.size()==1){ List userLists = father.get(0).elements(); if (userLists!=null&&!list.isEmpty()) { lists = new ArrayList(); for (Element e : userLists) {

List li = e.elements();

Class cl = (Class) Class.forName(cls.getName()); Object ob = cl.newInstance(); for (Element element2 : li) {

String name = element2.getName(); String value = element2.getText();

Field field = ob.getClass().getDeclaredField(name); field.setAccessible(true);

convertValue(field, ob, value); }

lists.add(ob); } } } }

}

return lists; } /**

* 解析xml⽂件返回保存cls的List集合,如果返回码resultCode=1时则返回List为null *

* @param url * @param cls

* @return

* @throws Exception */

@SuppressWarnings(\"unchecked\")

public static List parseXml2List(URL url, Class cls) throws Exception { List lists = null;

SAXReader saxReader = new SAXReader(); Document doc = saxReader.read(url); Element et = doc.getRootElement(); String root = et.getName(); // 查看返回码是否为真.

List list = doc.selectNodes(\"//\" + root + \"/resultCode\"); if (!list.isEmpty() && list.size() > 0) { Element element = list.get(0);

String returnResult = element.getText(); if (returnResult.equals(\"0\")) {

List father = doc.selectNodes(\"//\" + root + \"/\"+ cls.getSimpleName()+\"s\"); //判断对象⽗节点是否有包含数据

if(father!=null&&!father.isEmpty()&&father.size()==1){ List userLists = father.get(0).elements(); if (userLists!=null&&!list.isEmpty()) { lists = new ArrayList(); for (Element e : userLists) {

List li = e.elements();

Class cl = (Class) Class.forName(cls.getName()); Object ob = cl.newInstance(); for (Element element2 : li) {

String name = element2.getName(); String value = element2.getText();

Field field = ob.getClass().getDeclaredField(name); field.setAccessible(true);

convertValue(field, ob, value); }

lists.add(ob); } } } }

}

return lists; } /**

* 解析xml⽂件返回保存Map的集合,map中可能包含key值为returnCode、desc、totalCount等单字段. * 也可能包含存储对象为List的集合值. * 获取List值key cls_List * @param requestPath * @param cls * @return map

* @throws Exception */

@SuppressWarnings(\"unchecked\")

public static Map parseXml2Map(String requestPath, Class cls) throws Exception {

Map maps = new HashMap(); List lists = new ArrayList(); SAXReader saxReader = new SAXReader();

//Document doc = saxReader.read(new File(requestPath)); Document doc = saxReader.read(new URL(requestPath)); Element et = doc.getRootElement(); // 标记List是否为空 // boolean bool = true ; // 根节点名字

List rList = et.elements(); for (Element element : rList) {

List rLists = element.elements();

if (!rLists.isEmpty() && rLists.size() > 0) { //bool = false; // 判断⼆级节点

for (Element e : rLists) {

List li = e.elements();

Class cl = (Class) Class.forName(cls.getName()); Object ob = cl.newInstance(); for (Element element2 : li) {

String name = element2.getName(); String value = element2.getText();

Field field = ob.getClass().getDeclaredField(name); field.setAccessible(true);

convertValue(field, ob, value); }

lists.add(ob); } } else {

maps.put(element.getName(), element.getText()); }

maps.put(cls.getSimpleName() + \"_List\ }

return maps; } /**

* 只获取返回码0为保存成功(true)1为保存失败(false) */

@SuppressWarnings(\"unchecked\")

public static boolean parseXmlReturnCode(String xml) { boolean bool = false; try {

Document doc = DocumentHelper.parseText(xml); Element et = doc.getRootElement(); String root = et.getName(); // 查看返回码是否为真.

List list = doc.selectNodes(\"//\" + root + \"/resultCode\"); if (!list.isEmpty() && list.size() > 0) { Element element = list.get(0);

String returnResult = element.getText(); if (returnResult.equals(\"0\")) { bool = true; } }

} catch (Exception e) { e.printStackTrace(); }

return bool; }}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 版权所有