简体   繁体   English

python xml 在不同节点上基于多个条件过滤

[英]python xml filter based on multiple conditions on different nodes

I have the following xml data:我有以下 xml 数据:

<?xml version="1.0"?>
<Company>
  <Employee1>
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>tanmaypatil@xyz.com</Email>
      <Address>
           <City>Bangalore</City>
      </Address>
      <name> XXXXX</name>
  </Employee1>
    <Employee2>
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>tanmaypatil@xyz.com</Email>
      <Address>
           <City>Chennai</City>
      </Address>
      <name> YYYYYY</name>
  </Employee2>
    <Employee3>
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>tanmaypatil@xyz.com</Email>
      <Address>
           <City>Bangalore</City>
      </Address>
      <name> ZZZZZ</name>
  </Employee3>
</Company>

I want to filter based on, City = Bangalore and get relevant contents of name tags for each.我想根据 City = Bangalore 进行过滤,并获取每个名称标签的相关内容。

The desired output when filtered City = Bangalore:过滤城市 = 班加罗尔时所需的 output:

        <name> XXXXX</name>
        <name> ZZZZZ</name>

I have tried using the below and nothing helped me:我尝试使用以下内容,但没有任何帮助:

import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
tree.findall('city=Bangalore').name

But did not get what I am trying.但没有得到我正在尝试的东西。 Can someone help please?有人可以帮忙吗?

Try to use XPath:尝试使用 XPath:

'//Employee[Address/City="Bangalore"]/name'

I had a similar problem like you but used minidom.我和你有类似的问题,但使用了 minidom。 I wrote two small functions to solve it.我写了两个小函数来解决它。 I wrote a some Code based on your original xml.我根据您原来的 xml 编写了一些代码。 This gives a list with all nodes containing Bangalore.这给出了包含班加罗尔的所有节点的列表。

from xml.dom import minidom

def findChild(parent, childLocalName):
    for child in parent._get_childNodes():
        if child._get_localName() == childLocalName:
            return True, child
    return False, child

def followXMLPath(parent, path):# path should be a list of node names as string
    if path != None:
        for localName in path:
            result = findChild(parent, localName)
            if result[0] == True:
                parent = result[1]
            else:
                return result[0], parent
        return result
    else:
        return "error", parent

if __name__ == "__main__":
    xml = "C:\\Users\\AJ2MSGR\\Downloads\\bangalor.txt"
    xmldoc = minidom.parse(xml)
    finds = xmldoc.getElementsByTagName('Employee')

    bangaloreEmployees = []
    for element in finds:
        searchResult = followXMLPath(element,["Address", "City"]) #gives back a tuple
        success = searchResult[0]
        cityNode = searchResult[1]
        if success :
            if cityNode.firstChild.data == "Bangalore":
                bangaloreEmployees.append(element)
        else:
            print("nope")

    for element in bangaloreEmployees:
        print("::")
        print(findChild(element,"ContactNo")[1].firstChild.data)
        print(findChild(element,"name")[1].firstChild.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM