使用dom4j解析xml工具类

使用dom4j解析xml


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

<dependency>
       <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6</version>
 </dependency>

附上基于dom4j解析xml的工具类

/*
 * 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放在常量里面。
注意点:

Method method = obj.getClass().getMethod("set"+propertyname,String.class);
method.invoke(obj,propertyvalue);

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值