查找xml中的接口名及涉及表名并输出

#! /usr/bin/env python3

# -*- coding:utf-8 -*-

import xml.dom.minidom  #该模块被用来处理xml文件

import re #正则表达式模块

dom=xml.dom.minidom.parse('e:/pcf10.xml') #打开xml文档,          * * *请确保E盘下有文件pcf10.xml  *  *  *

#xml.dom.minidom模块被用来处理xml文件,并将这个文件对象赋值给dom变量。

 #得到文档元素对象

root=dom.documentElement

#documentElement用于得到dom对象的文档元素,并把获得的对象给root

#获得子标签

#对于知道元素名字的子元素,可以使用getElementsByTagName方法获取:

#root.getElementsByTagName('select')获得的是标签为select的一组标签

itemlist=root.getElementsByTagName('select')

modl=re.compile('(pgenius.)([a-z_A-Z]{2,50})') #正则表达式匹配模式

tableset=set()  #集合变量,不含重复值

f=open('e:/w.txt','w')  #创建并打开文件w.txt ,用于将写入结果集。

for i in itemlist:

    tableset1=set()

    tableset2=set()

    content=[n.data.strip() for n in i.childNodes if n.nodeType==4][0]

    tabless=modl.findall(content) #符合匹配模式的结果集

    for n,m in tabless:

        tableset1.add(n)

        tableset2.add(m)

        tableset.add(m)

    if len(tableset2):

        print(i.getAttribute("id"))  #getAttribute方法可以获得元素的属性所对应的值。

        print(tableset1)

        f.write(i.getAttribute("id"))

        f.write(' ')

        print(tableset2)

    tableset1=list(tableset1)

    tableset2=list(tableset2)

    if len(tableset1):

        f.write(tableset1[0])

        f.write(' ')     

    for j in tableset2:

        f.write(j)

        f.write(' ')

print(tableset)  #此列表为xml文件中所有符合条件的表的无重复集合,未写入文件w.text,仅在显示界面输出。

f.close()

原文地址:https://www.cnblogs.com/Ting-light/p/9548219.html