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循环遍历,太费事并且还效率低!!找到这中实现!!记录一下

有关这个方法的详细讲解参考如下:

https://blog.csdn.net/hekaihaw/article/details/54376656

总之这个方法很强大,后面用到之后再细研究。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值