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;
    }
}

猜你喜欢

转载自blog.csdn.net/xinyu100100/article/details/80633185
今日推荐