import xml.etree.ElementTree as ET
filename = 'script-tvguide-mainmenu.xml'
tree = ET.parse(filename)
root = tree.getroot()
controls = root.find('controls')
for control in controls.findall('control'):
#how do you create the if statement to check for the image through on xml if they are exist?
# Here are the image filenames, focus and nofocus.
focus = control.find('texturefocus').text
nofocus = control.find('texturenofocus').text
print('texturefocus={0}, texturenofocus={1}'.format(focus, nofocus))

Are you asking us to recommend an XML parser? There are a few in the stdlib, and popular third-party libraries like lxml. Stack Overflow is not a good site for recommendations on which library to use, or similar subjective questions—but if you pick any of those, try to write the code yourself, and get stuck, it's a great place to get help to get unstuck.
–
abarnertJan 9 '14 at 20:16

As an aside, why is this tagged both python-2.7 and python-3.x? Are you trying to write code that works with both versions? Or using one version but open to switching if it makes your life easier? Or just not sure which one you're using?
–
abarnertJan 9 '14 at 20:17

@abarnert yes I do. I have XML parser, but I just want to know how do you write to make the if statement include the xml parser in python that if I have the image called image 4.jpg then i can change the image using the xml id? please see the xml parser in my update post.
–
Chris JohnsonJan 9 '14 at 20:21

@abarnert Sorry, I'm using xmbc so i don't know which one of them (python-2.7 or python-3.x) is the one that work for xbmc?
–
Chris JohnsonJan 9 '14 at 20:22

Meanwhile, it looks like XBMC uses Python 2.6, so both of your tags are wrong. More generally, if you have no idea which version you're using, just use the python tag and don't try to guess and add tags that may be wrong.
–
abarnertJan 9 '14 at 20:25

1 Answer
1

Here's the simplest way to adapt your code to find the control whose texture is Image 2.jpg:

First, you're looking for an element named texturefocus. But in your XML sample, there is no such element—and even if there were, the one you're looking for is named texture. So obviously you need to fix that:

texture = control.find('texture').text

Second, you're looking for an image Image 2.jpg, but there is no such image in your XML, so you're not going to find it. There is an Image 2.png, but that's not the same thing. So, presumably you need to fix that as well.

And now, the if statement is trivial:

if texture == 'Image 2.png':

The question is, what do you want to do when you find it? Just printing out a string isn't going to help the rest of your code use that value.

Let's say that what you want to do is to write a function that returns the description if there's an image whose texture is Image 2.png, or returns None otherwise. Then: