循环解析xml dom

前端之家收集整理的这篇文章主要介绍了循环解析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
原文链接:https://www.f2er.com/xml/293876.html

猜你在找的XML相关文章