Python For loop display previous value if correct one match with XML

Question:

right now I have this XML

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

this is the code I’m 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 I’m searching, and the value is enabled=True, but I need to get the one behind this (android.widget.LinearLayout) with the value enabled=False

Asked By: Ibm Ibm

||

Answers:

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
Answered By: Jab
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.