JAVA工具类(8)- 读取XML文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class TestXml {

    public static void main(String[] args) {
        //TestXml
        File file = new File("D:\\test.txt");
        String xml = readTxtFile(file.getAbsolutePath());
        System.out.println(xml);
        try {
            List<Map<String, Object>> returnList = parseMessageXml(xml);
        } catch (DocumentException e) {

            e.printStackTrace();
        }

    }


    public static String readTxtFile(String filePath) {
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { // 判断文件是否存在
                StringBuffer sbr = new StringBuffer();
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);// 考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    sbr.append(lineTxt);
                }
                read.close();
                return sbr.toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }

    }



    @SuppressWarnings("unchecked")
    private static   List<Map<String, Object>> parseMessageXml(
            String xml) throws DocumentException {
        List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
        Document doc = DocumentHelper.parseText(xml);
        List<Element> infoList = doc.selectNodes("//MbfBody//list//info");
        if (CollectionUtils.isNotEmpty(infoList)) {
            Map<String, Object> tmpDto;
            for (Element element : infoList) {
                tmpDto = new HashMap<String, Object>();
                tmpDto.put("name", getValue(element, "name"));
                returnList.add(tmpDto);
            }

        } else {

        }

        return returnList;
    }


    /**
     * 获取文本
     * 
     * @param element
     * @param name
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String getValue(Element element, String name) {
        Iterator it = element.elementIterator(name);
        return getText(it);
    }

    /**
     * 获取文本
     * 
     * @param it
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static String getText(Iterator it) {
        if (it.hasNext()) {
            Element el = (Element) it.next();
            if (null != el && null != el.getText()) {
                return el.getText();
            }
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值