Home > Software engineering >  Python For loop display previous value if correct one match with XML
Python For loop display previous value if correct one match with XML

Time:08-27

right now i have this XML

I need to get 1 value ("enabled") behind the correct one i get.

this is the code im using

def checktap(text):
  nodes = minidom.parse("file.xml").getElementsByTagName("node")
  for node in nodes:
      if node.getAttribute("text") == text:

          if node.getAttribute("enabled") == True:
              return ("Yes")
          else:
              return ("No")       


value = checktap("EU 39")
print(value)
    

with this code, i'll get the exact node im searching, and the value is enabled=True, but i need to get the one behind this (android.widget.LinearLayout) with the value enabled=False

CodePudding user response:

You can use itertools.pairwise

from itertools import pairwise

def checktap(text):
    nodes = minidom.parse("file.xml").getElementsByTagName("node")
    for node1, node2 in pairwise(nodes):
        if node2.getAttribute("text") == text and node2.getAttribute("enabled"):
            return True
    return False
  • Related