SaxReader解析xml文件--绕过多层for循环

我的xml文件格式:

<?xml version="1.0" encoding="UTF-8"?>
<tclscripts id="1175269" loadmod="ZteTTATUI" loadfun="RefreshLSuite" TclScripts="D:/ATest1.0/Autotest/tclscript" whosave="ZteTTATLSuite:BuildProtoFileInfo" vts="8444" vtm="2019-11-14 20:22:10" database="BJAutoRepo1120">
	<testmodule id="1118082" name="ZXR10_9900_V2.00.00R4" path="ZXR10_9900_V2.00.00R4">
		<testprotocol id="1177069" name="MPLS L3VPN(R4)" path="MPLS L3VPN(R4)" scriptpath="Tests" algorpath="Algorithm" confpath="Configs" docpath="Docs" shimpath="MPLS L3VPN(R4)_ShimFile.tcl" despath="Mac_Des.doc" tagdtb="BJAutoRepo1120" tagwho="6092002531" tagauthor="6092001042" version_shime="101">
			<testgroup id="1177118" name="Group03 基本功能" path="Group03 基本功能" cfgpath="Group03 基本功能_Configure.tcl" telgroupconf="Group03 基本功能_TelConfig.txt" telgroupdisconf="Group03 基本功能_TelDisConfig.txt" despath="Group03 基本功能.doc" toppath="Group03 基本功能.top" tagdtb="BJAutoRepo1120" tagwho="6092002531" tagauthor="常建妹6092001042" version_config="101" version_telnet="101" version_distelnet="101" version_topology="101">
	        <testscript id="1177362" name="端口(带业务)频繁闪断(shut/no shut模拟)" path="TC-MPLS L3VPN-Q-STA-003100.tcl" telconf="TC-MPLS L3VPN-Q-STA-003100_TelConfig.txt" teldisconf="TC-MPLS L3VPN-Q-STA-003100_TelDisConfig.txt" despath="TC-MPLS L3VPN-Q-STA-003100.doc" version_script="101" version_telnet="101" version_distelnet="101"/>

			<testscript id="1177406" name="L3VPN支持的最大VRF数量" path="TC-MPLS L3VPN-Q-CAP-000100.tcl" telconf="TC-MPLS L3VPN-Q-CAP-000100_TelConfig.txt" teldisconf="TC-MPLS L3VPN-Q-CAP-000100_TelDisConfig.txt" despath="TC-MPLS L3VPN-Q-CAP-000100.doc" version_script="101" version_telnet="101" version_distelnet="101"/>
			</testgroup>
		</testprotocol>
		<testprotocol>
			<testgroup id="1178742" name="Group 01 基本功能测试" path="Group 01 基本功能测试" cfgpath="Group 01 基本功能测试_Configure.tcl" telgroupconf="Group 01 基本功能测试_TelConfig.txt" telgroupdisconf="Group 01 基本功能测试_TelDisConfig.txt" despath="Group 01 基本功能测试.doc" toppath="Group 01 基本功能测试.top" tagdtb="BJAutoRepo1120" tagwho="6092002531" tagauthor="10240406" version_config="101" version_telnet="101" version_distelnet="101" version_topology="101">
		
				<testscript id="1178995" name="VSC 0机框上电(拷机测试)" path="TC-MPLS LDP-Q-STA-002500.tcl" telconf="TC-MPLS LDP-Q-STA-002500_TelConfig.txt" teldisconf="TC-MPLS LDP-Q-STA-002500_TelDisConfig.txt" despath="TC-MPLS LDP-Q-STA-002500.doc" version_script="101" version_telnet="101" version_distelnet="101"/>
				<testscript id="1178997" name="主备倒换(拷机测试)" path="TC-MPLS LDP-Q-STA-002600.tcl" telconf="TC-MPLS LDP-Q-STA-002600_TelConfig.txt" teldisconf="TC-MPLS LDP-Q-STA-002600_TelDisConfig.txt" despath="TC-MPLS LDP-Q-STA-002600.doc" version_script="101" version_telnet="101" version_distelnet="101"/>
			</testgroup>
		</testprotocol>
	</testmodule>
</tclscripts>

我的需求是提取每个组下面的脚本编号(path标签的值)对应脚本名称(name标签的值)

不多说直接代码:

package com.zte.st.services;

import java.io.File;
import org.apache.bcel.generic.NEW;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

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

/**
 * Created by Administrator on 2019/11/15.
 */
public class ParseXml {
    public static  Map<String,String> getResult(String url) throws DocumentException {
        Map<String,String> hashMap = new HashMap<>();
        String[] split = url.split("ZXR10_9900_V2.00.00R4");
        String s = split[1];
        String[] split1 = s.split("\\\\");
        String filename = split1[split1.length - 1];
        //XML文件的位置
        File file = new File("src\\main\\resources\\com\\zte\\st\\allscript.xml");

        System.out.println("file===>"+file);
        // 创建SAXReader
        SAXReader reader = new SAXReader();
        // 用SAXReader来读取文件,并转换成Document
        Document document = reader.read(file);
        //用Document的selectNodes来读取节点,返回list
        List<Node> list = document.selectNodes("/tclscripts/testmodule/testprotocol/testgroup");

        for(Node node:list){

            Element e = (Element)node;
            System.out.println("e的内容为:" + e);
            Attribute nameAttr = e.attribute("name");
//            System.out.println("属性为:"+nameAttr);

//            // 通过属性对象拿到属性名
////            String nameAttrName = nameAttr.getName();
//            // 通过属性对象拿到属性值
            String nameValue = nameAttr.getValue();
            System.out.println("组名:"+nameValue);

            List<Element> elements = e.elements();

            for(Element element1:elements){

                Attribute nameAttr1 = element1.attribute("name");

                String  nameValue1 =  nameAttr1.getValue();
                System.out.println("脚本名称:" + nameValue1);

                Attribute nameAttr2 = element1.attribute("path");

                String nameValue2 = nameAttr2.getValue();
                System.out.println("脚本编号:" + nameValue2);
                String key = nameValue+nameValue2;
                hashMap.put(key,nameValue1);
            }

        }

        String groupName = split1[3];

        return  hashMap;
    }
}

 原先用的本方法,一层层for循环遍历,太费事并且还效率低!!找到这中实现!!记录一下

发布了23 篇原创文章 · 获赞 12 · 访问量 9536

猜你喜欢

转载自blog.csdn.net/geng2568/article/details/103091106