XMLParse.java 6.8 KB
package com.example.demo.util.XML;

import ch.qos.logback.core.joran.spi.XMLUtil;
import com.example.demo.DemoApplication;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

public class XMLParse {
    private static String xmlString;
    private static String[] nodes;
    private static Document document;
    private static SAXBuilder builder;
    private static Element root;
    private static LinkedList<SubNode> listNode;
    private static StringReader xmlReader;
    private static InputSource xmlSource;
    private String value;
    public XMLParse() {
    }
    Logger logger = LoggerFactory.getLogger(DemoApplication.class);
    /**
     *
     * @param xmlString xml字符窜
     * @param xmlNodePath 节点参数(例如:/root/head/type)
     */
    public XMLParse(String xmlString, String xmlNodePath) {
        this.xmlString = xmlString;
        this.nodes = xmlNodePath.split("/");
        this.builder = new SAXBuilder();
        this.root = null;
        this.listNode = new LinkedList();
        this.value = "";
        this.document =new Document();
    }

    /**
     * 取一对多关系的节点下的所有子节点的属性名 和 值
     * @return
     * @throws Exception
     */
    public  List<Map> getNodeValuesFromXmlString() throws Exception{

        try {
            this.xmlReader = new StringReader(this.xmlString);
            this.xmlSource = new InputSource(this.xmlReader);
            this.document = this.builder.build(xmlSource);
            List<Map> maps = new ArrayList<Map>();
            for(int i = 1; i < nodes.length; ++i) {
                SubNode snode;

                if (i >1) {
                    snode = (SubNode)listNode.getLast();
                    List<Element> eList = snode.getElement().getChildren(nodes[i]); //这里等于3的时候得到associatedPary节点 两个,下面应该循环这两个element,取他下面的所有children
                    if (eList != null && eList.size() > 0) {
                        SubNode s1 = new SubNode((Element)eList.get(0));
                        listNode.add(s1);

                    }
                    if(eList != null && eList.size() > 1 && i==nodes.length-1){ //只有进入的最尾节点的时候再循环去取同名下的一对多的ELEMENT所有子节点

                        for (Element element : eList){
                            List<Element> elements=element.getChildren();
                            Map<String,String> childMap = new HashMap<String, String>(); //用map存储每个值
                                for (Element childElements : elements){
                                    String childName = childElements.getName();
                                    String childValue = childElements.getText();
                                    childMap.put(childName,childValue);
                                    logger.debug(childValue);
                                }
                        maps.add(childMap);
                        }
                    }

                } else if (1 == i) {
                    root = document.getRootElement();
                    snode = new SubNode(root);
                    listNode.add(snode);
                }

            }
            return maps;
        } catch (Exception var17) {
            var17.printStackTrace();
            throw var17;
        } finally {
            listNode.clear();
            document = null;
            root = null;
            builder = null;
            nodes = null;
        }

    }

    /***
     * 获取单一节点的值
     * @return 单一节点值
     * @throws Exception
     */
    public  final String getNodeValueFromXmlString() throws Exception {
        try {
            this.xmlReader = new StringReader(this.xmlString);
            this.xmlSource = new InputSource(this.xmlReader);
            this.document = this.builder.build(xmlSource);

            for(int i = 1; i < nodes.length; ++i) {
                SubNode snode;
                if (i > 1) {
                    snode = (SubNode)listNode.getLast();
                    List<Element> eList = snode.getElement().getChildren(nodes[i]);
                    if (eList != null && eList.size() > 0) {
                        SubNode s1 = new SubNode((Element)eList.get(0));
                        listNode.add(s1);
                    }
                } else if (1 == i) {
                    root = document.getRootElement();
                    snode = new SubNode(root);
                    listNode.add(snode);
                }
            }

            SubNode n = (SubNode)listNode.getLast();
            value = n.getElement().getText();
            n = null;
            return value;
        } catch (Exception var17) {
            var17.printStackTrace();
            throw var17;
        } finally {
            listNode.clear();
            document = null;
            root = null;
            builder = null;
            nodes = null;
        }

    }

    public static final String getNodeAttribute(String xmlFile, String xmlNodePath, String attributeName, String defaultValue) {
        String[] nodes = xmlNodePath.split("/");
        new Document();
        SAXBuilder builder = new SAXBuilder();
        Element root = null;
        LinkedList<SubNode> listNode = new LinkedList();
        String value = "";

        Document document;
        try {
            document = builder.build(xmlFile);

            for(int i = 1; i < nodes.length; ++i) {
                SubNode snode;
                if (i > 1) {
                    snode = (SubNode)listNode.getLast();
                    List<Element> eList = snode.getElement().getChildren(nodes[i]);
                    if (eList != null && eList.size() > 0) {
                        SubNode s1 = new SubNode((Element)eList.get(0));
                        listNode.add(s1);
                    }
                } else if (1 == i) {
                    root = document.getRootElement();
                    snode = new SubNode(root);
                    listNode.add(snode);
                }
            }

            SubNode n = (SubNode)listNode.getLast();
            value = n.getElement().getAttributeValue(attributeName, defaultValue);
            n = null;
        } catch (JDOMException var18) {
            var18.printStackTrace();
        } catch (IOException var19) {
            var19.printStackTrace();
        } finally {
            listNode.clear();
            document = null;
            root = null;
            builder = null;
            nodes = null;
        }

        System.out.println("key=" + xmlNodePath + " node attrivte value=" + value);
        return value;
    }
}