审查视图

src/main/java/com/example/demo/util/XML/XMLParse.java 11.9 KB
朱兆平 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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 {
朱兆平 authored
18
    private  static  final Logger logger = LoggerFactory.getLogger(DemoApplication.class);
朱兆平 authored
19 20 21 22 23 24 25
    private static String xmlString;
    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;
朱兆平 authored
26 27 28
    private static List<Map> maps = new ArrayList<Map>();
    private static Map<String,Map> allElementValue = new HashMap<String, Map>();
    private static int domCount=0;
朱兆平 authored
29
    private String value;
朱兆平 authored
30
朱兆平 authored
31 32
    public XMLParse() {
    }
朱兆平 authored
33 34 35 36 37 38 39
    public XMLParse(String XmlString) {
        builder = new SAXBuilder();
        root = null;
        listNode = new LinkedList();
        value = "";
        document =new Document();
        xmlString = XmlString;
朱兆平 authored
40 41
    }
朱兆平 authored
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    public Map getAllValuesFromXmlString() throws  Exception{
        try {
            xmlReader = new StringReader(xmlString);
            xmlSource = new InputSource(xmlReader);
            document = builder.build(xmlSource);
            root = document.getRootElement();
            getElements(root);

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

        }
        return allElementValue;
    }

    public void getElements (Element element) throws  Exception{
        try {
            domCount++;
            List<Element> Children = element.getChildren();
            if (Children.size()>0){
                for (Element elements : Children){
                    if(elements.getChildren().size()>0){
                        getElements(elements);
                    }else {//确定是最末节点了 开始取末节点的数据
                        String childName = elements.getName();
                        String childValue = elements.getText();
                        Map<String,String> childMap = new HashMap<String, String>(); //用map存储每个最终节点的值
                        childMap.put(childName,childValue);
                        allElementValue.put(domCount+elements.getParentElement().getName()+"-"+childName,childMap);
                    }
                }
            }


        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }

    }
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    public  List<Map> getNodeValuesFromXmlStringForPffm(String xmlNodePath) throws Exception{
        String[] nodes = xmlNodePath.split("/");
        try {
            xmlReader = new StringReader(xmlString);
            xmlSource = new InputSource(xmlReader);
            document = 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]); //这里得到所有Arrival节点 从ArrivalEvent开始取
                    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){ //开始取出每个ArrivalEven每个字节点
                            List<Element> elements=element.getChildren();
                            Map<String,String> childMap = new HashMap<String, String>(); //用map存储AssociatedTransportCargo节点下每个值
                            for (Element childElements : elements){
                                if (childElements.getChildren().size()>1){
                                    for (Element grandChildrenEle : (List<Element>)childElements.getChildren()){ //IncludedMasterConsignment重复节点
                                        String childName = grandChildrenEle.getName();
                                        String childValue = grandChildrenEle.getText();
                                        childMap.put(childName,childValue);
                                    }
                                }
                                String childName = childElements.getName();
                                String childValue = childElements.getText();
                                childMap.put(childName,childValue);
                                logger.info(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;
        }

    }
朱兆平 authored
150
    /**
151 152
     * 取一对多关系的节点下的所有子节点的属性名 和 值,//这个方法作为FWB解析使用
     * @return 返回重复节点下的所有节点数据
朱兆平 authored
153 154
     * @throws Exception
     */
朱兆平 authored
155 156
    public  List<Map> getNodeValuesFromXmlString(String xmlNodePath) throws Exception{
        String[] nodes = xmlNodePath.split("/");
朱兆平 authored
157
        try {
朱兆平 authored
158 159 160
            xmlReader = new StringReader(xmlString);
            xmlSource = new InputSource(xmlReader);
            document = builder.build(xmlSource);
朱兆平 authored
161 162 163 164 165 166 167 168 169 170 171 172 173 174
            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所有子节点
175
                        for (Element element : eList){ //开始取出每个字节点
朱兆平 authored
176 177 178
                            List<Element> elements=element.getChildren();
                            Map<String,String> childMap = new HashMap<String, String>(); //用map存储每个值
                                for (Element childElements : elements){
朱兆平 authored
179 180 181 182 183 184 185
                                    if (childElements.getChildren().size()>1){
                                        for (Element grandChildrenEle : (List<Element>)childElements.getChildren()){
                                            String childName = grandChildrenEle.getName();
                                            String childValue = grandChildrenEle.getText();
                                            childMap.put(childName,childValue);
                                        }
                                    }
朱兆平 authored
186 187 188
                                    String childName = childElements.getName();
                                    String childValue = childElements.getText();
                                    childMap.put(childName,childValue);
朱兆平 authored
189
                                    logger.info(childValue);
朱兆平 authored
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
                                }
                        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;
朱兆平 authored
210
//            builder = null;
朱兆平 authored
211 212 213 214 215 216 217 218 219 220
            nodes = null;
        }

    }

    /***
     * 获取单一节点的值
     * @return 单一节点值
     * @throws Exception
     */
朱兆平 authored
221 222
    public  final String getNodeValueFromXmlString(String xmlNodePath) throws Exception {
        String[] nodes = xmlNodePath.split("/");
朱兆平 authored
223
        try {
朱兆平 authored
224 225 226
            xmlReader = new StringReader(xmlString);
            xmlSource = new InputSource(xmlReader);
            document = builder.build(xmlSource);
朱兆平 authored
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

            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;
朱兆平 authored
255
//            builder = null;
朱兆平 authored
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
            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;
    }
}