前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用dom4j解析xml工具类[通俗易懂]

使用dom4j解析xml工具类[通俗易懂]

作者头像
全栈程序员站长
发布2022-08-31 16:32:15
8180
发布2022-08-31 16:32:15
举报

大家好,又见面了,我是你们的朋友全栈君。

使用dom4j解析xml

首先在项目中加入dom4j的依赖

代码语言:javascript
复制
<dependency>
       <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6</version>
 </dependency>

附上基于dom4j解析xml的工具

代码语言:javascript
复制
/* * Copyright © 1998 - 2018 Tencent. All Rights Reserved * www.tencent.com * All rights reserved. */
package com.tencent.tusi.iot.utils;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.tencent.tusi.iot.common.PublicConfig.*;

/** * @author shanpeng */
// 解析xml字符串的工具类
public class XMLUtils{ 
   
    /** * 获取xml对象的根节点 * @param document * @return root */
    public static Element getRoot(Document document){ 
   
        Element root = null;
        root = document.getRootElement();
        return root;
    }
    /** * 获取fieldName的属性值 * @param document * @return fields */
    public static List<String> getFields(Document document){ 
   
        List<String> fields = new ArrayList<>();
        Element root = getRoot(document);
        if (null != root){ 
   
            // 存储遍历节点
            for (Iterator i = root.elementIterator(); i.hasNext();) { 
   
                Element el = (Element) i.next();
                for (Iterator j = el.elementIterator(FIELD); j.hasNext();){ 
   
                    Element e2 = (Element) j.next();
                    Attribute attribute = e2.attribute(FIELDNAME);
                    fields.add(attribute.getValue());
                }
            }
        }
        return fields;
    }

    /** * 根获取每行数据形成一个列表 * @param document * @return rows */
    public static List<Element> getRows(Document document){ 
   
        List<Element> rows = new ArrayList<>();
        Element root = getRoot(document);
        List<String> fields = getFields(document);
        if (!fields.isEmpty()){ 
   
            for (Iterator i = root.elementIterator(); i.hasNext();) { 
   
                Element el = (Element) i.next();
                for (Iterator k = el.elementIterator(ROW);k.hasNext();){ 
   
                    // 一行数据
                    Element e2 = (Element) k.next();
                    rows.add(e2);
                }
            }
        }
        return rows;
    }

    /** * 根据url获取xml对象 * @param url * @return document */
    public static Document getXmlFromUrl(String url) throws DocumentException{ 
   
        Document document=null;
        SAXReader saxReader = new SAXReader();
        document = saxReader.read(url);
        return document;
    }

    /** * 根据xml字符串获取xml对象 * @param xmlString * @return document */
    public static Document getXmlFromString(String xmlString) throws DocumentException { 
   
        Document document = DocumentHelper.parseText(xmlString);
        return document;
    }

    /** * xml一行转对象 * @param document * @param clazz * @param row * @return * row ---> pojo */
    public static Object getObject(Document document,Class<?> clazz,Element row) { 
   
        Object obj=null;
        try { 
   
            obj=clazz.newInstance();//创建对象
            List<String> fields = getFields(document);
		// 获取属性名
                for(int i=0;i<fields.size();i++){ 
   
                    String propertyname = fields.get(i);
                    String propertyvalue = row.attribute(fields.get(i)).getValue();
                    Method method = obj.getClass().getMethod("set"+propertyname,String.class);
                    method.invoke(obj,propertyvalue);
                }
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        return obj;
    }

    public static void main(String[] args) throws DocumentException { 
   
    }
    }
}

因为工作中接收的数据都是固定格式的,所以其中ROW、FIELD、FIELDNAME放在常量里面。 注意点:

代码语言:javascript
复制
Method method = obj.getClass().getMethod("set"+propertyname,String.class);
method.invoke(obj,propertyvalue);

该代码块中只能识别为String类型的属性,所以使用getObject时务必使传过来的类中的属性全部是String类型的,具体使用时再做转换。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152052.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用dom4j解析xml
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档