循环解析xml dom

最近有用到,简单写一下。不感知内容,递归调用。

package com.ferraborghini.parser;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlParser {
    public static void main(String[] args) throws SAXException, IOException,
            ParserConfigurationException {
        File xml = new File("conf/test.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xml);
        Element element = doc.getDocumentElement();
        Map<String, Object> result = getMap(element);
        int level = 0;
        printMap(result, level);
    }

    private static void printMap(Map<String, Object> result, int level) {
        for (String key : result.keySet()) {
            if (result.get(key) instanceof Map) {
                System.out.println("key: "+ key);
                printMap((Map)result.get(key), ++level);
                level--;
            } else {
                for (int i = 0; i < level*5; i++) {
                    System.out.print(" ");
                }
                System.out.println("key: "+ key + " value: "+ result.get(key));
            }

        }
    }
    public static Map<String, Object> getMap(Node node) {
        Map<String, Object> domMap = new HashMap();
        if (node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                domMap.put(attrs.item(i).getNodeName(), attrs.item(i)
                        .getNodeValue());
//              System.out.println("attr name: " + attrs.item(i).getNodeName());
            }
        }
        NodeList nodelist = node.getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            Node child = nodelist.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                String result = getChild(child);
                if (result == null) {
                    domMap.put(child.getNodeName(), getMap(child));
                } else {
                    domMap.put(child.getNodeName(), result);
                }
            }
        }

        return domMap;
    }

    /**
     * 如果下一个节点是text node获取内容
     * @param node
     * @return
     */
    public static String getChild(Node node) {
        String result = null;
        NodeList nodelist = node.getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            if (nodelist.item(i).getNodeType() == Node.TEXT_NODE
                    && (nodelist.item(i).getTextContent().matches("\\S+"))) {
                result = nodelist.item(i).getTextContent();
            }
        }

        return result;
    }

}

网上随便找了一个xml文件:

<?xml version="1.0" encoding="UTF-8"?>  
<employees bb="bbbbb" yy="yyyyy">  
    <employee1 id="11" name="xiaowang">  
        <sex>man</sex>  
        <age>25</age>  
    </employee1>  
    <employee2 id="12" name="liyi">  
        <sex>woman</sex>  
        <age>45</age>  
    </employee2>  
</employees>  

打印结果:

key: bb value: bbbbb
key: yy value: yyyyy
key: employee1
     key: sex value: man
     key: name value: xiaowang
     key: id value: 11
     key: age value: 25
key: employee2
     key: sex value: woman
     key: name value: liyi
     key: id value: 12
     key: age value: 45
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值