美文网首页
遍历 xml 所有的节点

遍历 xml 所有的节点

作者: Canon_2020 | 来源:发表于2020-04-20 09:06 被阅读0次

element.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- 左侧菜单、通知 -->
<section class="sidebar" style="height: auto;">
    <div class="user-panel">
        <div class="pull-left image">
            <img style="height: 45px;width: 45px" class="img-circle" alt="User Image" src="/oms/showImgHead"></img>
        </div>
        <div class="pull-left info">
            <p>shangwu</p>
            <a href="#">
                <i class="fa fa-circle text-success"></i>
                Online
            </a>
        </div>
    </div>
    <!-- 菜单栏显示 -->
    <ul class="sidebar-menu">
        <!--遍历一级目录-->
        <li class="treeview" id="2222">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Home</span>
                <span class="pull-right-container" id="Home">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Homearrows"></i>
                </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/home/toHome?moduleId=2231" id="node-2231">
                        <i class="fa fa-circle-o"></i>
                        <span>Home</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2224">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i>
                <span>Task</span>
                <span class="pull-right-container" id="Task">
                    
      <small class="label pull-right bg-yellow">8</small></span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/task/toTask?moduleId=2232" id="node-2232">
                        <i class="fa fa-circle-o"></i>
                        <span>Task</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2245">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Ticket</span>
                <span class="pull-right-container" id="Ticket">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Ticketarrows"></i>
                </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/ticket/toTicket?moduleId=2246" id="node-2246">
                        <i class="fa fa-circle-o"></i>
                        <span>Ticket</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2223">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Merchant onboard</span>
                <span class="pull-right-container" id="Merchant onboard">
                    
      <small class="label pull-right bg-green">5</small><small class="label pull-right bg-blue">37</small></span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/merchant/toPreCheck?moduleId=2254" id="node-2254">
                        <i class="fa fa-circle-o"></i>
                        <span>PreCheck</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/merchant/toMerchantOnboard?moduleId=2233" id="node-2233">
                        <i class="fa fa-circle-o"></i>
                        <span>Onboard</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview active" id="2225">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Merchant</span>
                <span class="pull-right-container" id="Merchant">
                    
      <small class="label pull-right bg-green">1</small></span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/merchant/toMerContactManagementPage?moduleId=2226" id="node-2226">
                        <i class="fa fa-circle-o"></i>
                        <span>Contact</span>
                    </a>
                </li>
                <li class="active">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/merchantInfo/forwardMerchantInfoList?moduleId=2227" id="node-2227">
                        <i class="fa fa-circle-o"></i>
                        <span>Merchant Profile</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/merchant/toMerchantHandover?moduleId=2255" id="node-2255">
                        <i class="fa fa-circle-o"></i>
                        <span>Merchant Handover</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/merchant/toMerVisitSummaryPage?moduleId=2229" id="node-2229">
                        <i class="fa fa-circle-o"></i>
                        <span>Visiting Record</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/merchant/toPriceInformation?moduleId=2267" id="node-2267">
                        <i class="fa fa-circle-o"></i>
                        <span>Pricing Management</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2234">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Payment Method Info</span>
                <span class="pull-right-container" id="Payment Method Info">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Payment Method Infoarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/producePrice/toProducePrice?moduleId=2164" id="node-2164">
                        <i class="fa fa-circle-o"></i>
                        <span>Payment Produce Price</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/productInfo/toProductInfo?moduleId=2235" id="node-2235">
                        <i class="fa fa-circle-o"></i>
                        <span>Payment Method Info</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2236">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Underwriting</span>
                <span class="pull-right-container" id="Underwriting">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Underwritingarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/questionnaire/toQuestionnaire?moduleId=2237" id="node-2237">
                        <i class="fa fa-circle-o"></i>
                        <span>Questionnaire</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/industrymanagement/toIndustryManagement?moduleId=2238" id="node-2238">
                        <i class="fa fa-circle-o"></i>
                        <span>Industry Management</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/contract/toContractmanage?moduleId=2239" id="node-2239">
                        <i class="fa fa-circle-o"></i>
                        <span>Merchant Agreement</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2240">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Risk Control</span>
                <span class="pull-right-container" id="Risk Control">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Risk Controlarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/riskControl/toRiskControlProgram?moduleId=2241" id="node-2241">
                        <i class="fa fa-circle-o"></i>
                        <span>Risk Control Program</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/riskControl/toAbnormalNotice?moduleId=2242" id="node-2242">
                        <i class="fa fa-circle-o"></i>
                        <span>Abnormal Transaction Notice</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2243">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Product</span>
                <span class="pull-right-container" id="Product">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Productarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/product/toPartnerMgr?moduleId=2244" id="node-2244">
                        <i class="fa fa-circle-o"></i>
                        <span>Partner Management</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/product/toPartnerHandover?moduleId=2256" id="node-2256">
                        <i class="fa fa-circle-o"></i>
                        <span>Partner Handover</span>
                    </a>
                </li>
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/product/toProductDB?moduleId=2257" id="node-2257">
                        <i class="fa fa-circle-o"></i>
                        <span>Product Database</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2247">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Report</span>
                <span class="pull-right-container" id="Report">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Reportarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/report/toInternalPerformReport?moduleId=2248" id="node-2248">
                        <i class="fa fa-circle-o"></i>
                        <span>Performance Report</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2169">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Sales Management</span>
                <span class="pull-right-container" id="Sales Management">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Sales Managementarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/salesSolution/querySolution?moduleId=2170" id="node-2170">
                        <i class="fa fa-circle-o"></i>
                        <span>Solution</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2251">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Documentation</span>
                <span class="pull-right-container" id="Documentation">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Documentationarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/document/toDocumentsMgr?moduleId=2252" id="node-2252">
                        <i class="fa fa-circle-o"></i>
                        <span>Documentation</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2253">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Notification</span>
                <span class="pull-right-container" id="Notification">
                    <i class="fa fa-angle-left pull-right list-arrow" id="Notificationarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/announcement/toAnnouncement?moduleId=2258" id="node-2258">
                        <i class="fa fa-circle-o"></i>
                        <span>Notification</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2259">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>System Management</span>
                <span class="pull-right-container" id="System Management">
                    <i class="fa fa-angle-left pull-right list-arrow" id="System Managementarrows"></i>
      </span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/system/toNoticeSetting?moduleId=2260" id="node-2260">
                        <i class="fa fa-circle-o"></i>
                        <span>Notification Setting</span>
                    </a>
                </li>
            </ul>
        </li>
        <li class="treeview" id="2165">
            <a href="#" onclick="parentNodeRecord(event);iconChange(event)">
                <i class="fa fa-dashboard"></i> <span>Work Report</span>
                <span class="pull-right-container" id="Work Report">
                    
      <small class="label pull-right bg-green">1</small></span>
            </a>
            <!--遍历二级目录-->
            <ul class="treeview-menu">
                <li class="">
                    <a onclick="NodeRecord(event)" href="http://192.168.11.98:8880/oms/workReport/toWorkReportPage?moduleId=2166" id="node-2166">
                        <i class="fa fa-circle-o"></i>
                        <span>Work Report</span>
                    </a>
                </li>
            </ul>
        </li>
    </ul>
</section>

location_xml.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-04-15 09:00:00
# @Author  : Canon
# @Link    : https://www.python.org
# @Version : 3.6.1


import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom
import HTMLParser
import sys,os,string

# 获取标签的文本
# tree = minidom.parse("element.xml")
# p = tree.documentElement
# val = p.getElementsByTagName("span")
# print(val[0].firstChild.data)

# 获取所有节点
# html = open("element.xml", "r", encoding="utf-8").read()
# tagstack = []  
# class ShowStructure(HTMLParser.HTMLParser):  
#     def handle_starttag(self, tag, attrs):
#         tagstack.append(tag)

#     def handle_endtag(self, tag):
#         tagstack.pop()

#     def handle_data(self, data):  
#         if data.strip():
#             for tag in tagstack:
#                 sys.stdout.write('/'+tag)
#             sys.stdout.write(' >> %s\n' % data[:40].strip())
# ShowStructure().feed(html)


# 全局唯一标识
unique_id = 0


# 遍历所有的节点
def walkData(root_node, level, result_list, loca="/section"):
    global unique_id
    unique_id += 1
    temp_list =[unique_id, level, root_node.tag, root_node.attrib, "/{}".format(root_node.tag), root_node.text]
    result_list.append(temp_list)
    if unique_id != 1:
        temp_list[4] = loca
        temp_list[5] = root_node.text
    # 遍历每个子节点
    children_node = root_node.getchildren()
    count_up = 1
    if len(children_node) == 0:
        loca += "/{}[{}]".format(root_node.tag, count_up)
        return
    # 找出每个标签的路径
    for num_node in range(len(children_node)):
        count = 1
        for num in range(num_node):
            if children_node[num_node].tag == children_node[num].tag:
                count += 1
        count_up += 1
        new_loca = loca + "/{}[{}]".format(children_node[num_node].tag, count)
        walkData(children_node[num_node], level + 1, result_list, new_loca)
    return


def getXmlData(file_name, text):
    # 节点的深度从1开始
    level = 1
    result_list = []
    root = ET.parse(file_name).getroot()
    walkData(root, level, result_list)
    for result in result_list:
        if result[5] == text:
            loca_val = result[4]
    return loca_val


R = getXmlData("element.xml", "Merchant Profile")
print(R)

相关文章

  • 遍历 xml 所有的节点

    element.xml location_xml.py

  • Qt 遍历 xml

    遍历xml(打印节点名和属性) 【核心方法】 【调用】

  • xml

    dom 解析xml文件 获取根节点 遍历 查 保存 添加 删除 修改 XPath 使用XPath对xml文件的元素...

  • Groovy使用XmlSlurper解析XML文件

    注意事项:1、节点的属性需要遍历;2、可以直接用节点名称定位节点;参考文章:Groovy操作XML:https:/...

  • XML解析

    dom4j 元素 解析xml文件 获取根节点 遍历 查 保存 添加 删除 改 XPath 使用XPath对xml文...

  • DOM

    DOM: 2、遍历所有的子节点:

  • Android AsyncLayoutInflater源码解析

    通常我们会在布局中编写大量的View,而安卓在加载这些View的时候会遍历解析所有的xml节点,然后通过反射创建对...

  • appium-关于xml的知识

    XML DOM节点树 XML DOM将XML文档作为树结构,树结构称为一个节点树。所有的节点可以通过树访问,它们的...

  • 二叉树

    前序遍历:根节点 -- 左节点 -- 右节点 中序遍历:左节点 -- 跟节点 -- 右节点 后序遍历:左节点 --...

  • beautifulsoup 库(二)

    遍历方式 下行遍历 通过循环实现打印 上行遍历 平行遍历 所有的平行遍历发生在同一个父亲节点下 prettify ...

网友评论

      本文标题:遍历 xml 所有的节点

      本文链接:https://www.haomeiwen.com/subject/hkltwctx.html