Java解析xml文件之DOM

本篇博客主要使用实例讲解如何使用dom即系xml文件,不会对源码做分析。直接结合代码进行说明。
测试解析的xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<changeFileName>
    <transactionInfo>
        <detail name="sourceDirectory" value="C:\log"></detail>
        <detail name="destDirectory" value="C:\log"></detail>
        <detail name="timeSerialNumer" value="20180521"></detail>
        <detail name="indexSerialNumber" value="10"></detail>
        <detail name="databaseUser" value="zhuyuqiang"></detail>
        <detail name="tableName" value=""></detail>
        <detail name="userFileNameAsTableName" value="true"></detail>
        <detail name="userUM" value="zhuyuqiang296"></detail>
        <detail name="fileType" value=".sql"></detail>
        <detail name="splitSymbol" value="_"></detail>
    </transactionInfo>
</changeFileName>

看一下解析xml的方法:

    //方法这边传入的是待解析xml文件的文件名
    private static List<TransactionInfo> parserByDom(String configFile){
        //实例化一个DocumentBuilderFactory对象
        DocumentBuilderFactory dbf = new DocumentBuilderFactoryImpl();
        List<TransactionInfo> infos = new ArrayList<>();
        try {
            //通过使用BuilderFactory生成一个DocumentBuilder,通过使用DocumentBuilder解析xml文件生成一个Document
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(new File(configFile));
            //根据标签名,获取所有符合条件的元素
            NodeList nodes = document.getElementsByTagName("changeFileName");
            //对标签下面的所有子标签进行解析,每个子标签都代表对象的一个属性及其对应的属性值
            for(int i = 0;i<nodes.getLength();i++){
                Node node = nodes.item(i);
                NodeList transactionInfos = node.getChildNodes();
                for(int j = 1;j<transactionInfos.getLength();j+=3){
                    Node transactionInfo = transactionInfos.item(j);
                    NodeList properties = transactionInfo.getChildNodes();
                    TransactionInfo info = new TransactionInfo();
                    for(int m = 0;m < properties.getLength();m++){
                        Node property = properties.item(m);
                        NamedNodeMap propertyMap = property.getAttributes();
                        try {
                            String propertyName = propertyMap.getNamedItem("name").getNodeValue();
                            String propertyValue = propertyMap.getNamedItem("value").getNodeValue();
                            log.debug("当前设置的属性名:"+propertyName+",设置的属性值:"+propertyValue);
                            //根据解析出来的属性,对对象相应的属性进行赋值
                            populateBean(info,propertyName,propertyValue);
                        } catch (Exception e) {
                            log.info(e.getMessage());;
                        }
                    }
                    infos.add(info);
                }
            }
        } catch (ParserConfigurationException e) {
            log.info(e.getMessage());
        } catch (SAXException e) {
            log.info(e.getMessage());
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        return infos;
    }

    private static void populateBean(TransactionInfo info,String propertyName,String propertyValue)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = info.getClass().getDeclaredField(propertyName);
        if(!field.isAccessible()){
            field.setAccessible(true);
        }
        field.set(info,propertyValue);
    }

解析的主要通过获取根标签,通过根标签获取所有子标签,通过对子标签进行遍历,解析出xml的所有内容,

打印出的解析封装后的TransactionInfo 信息如下:

[ sourceDirectory = C:\log, destDirectory = C:\log , timeSerialNumber = 20180521 , indexSerialNumber = 10 , databaseUser = zhuyuqiang , tableName =  , userFileNameAsTableName = true , userUM = zhuyuqiang296 , fileType = .sql , splitSymbol = _

通过打印的信息,方法已经将xml的所有信息成功解析了。

猜你喜欢

转载自blog.csdn.net/u011043551/article/details/80463296