Search code examples
pythonxmlxbmc

xml parser in python for xbmc


I would like to get your help with my .py script.

I'm using xml file to store the parser path for the images.

Here's the xml file that I use:

<?xml version="1.0" encoding="utf-8"?>
<window type="dialog">
    <allowoverlay>no</allowoverlay>
<coordinates>
<system>1</system>
<posx>0</posx>
<posy>0</posy>
</coordinates>

<controls>
    <control type="image" id="1">
       <posx>0</posx>
       <posy>0</posy>
       <width>1280</width>
       <height>720</height>
       <texture>background-defeat.png</texture>
       <animation effect="fade" start="0" end="100" time="6500">WindowOpen</animation>
    </control>

     <control type="image" id="2">
      <description>Image 2</description>
      <posx>307</posx>
      <posy>7</posy>
      <width>154</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 2.png</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>    

     <control type="image" id="3">
      <description>Image 3</description>
      <posx>460</posx>
      <posy>7</posy>
      <width>188</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 3.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>

    <control type="image" id="4">
      <description>Image 4</description>
      <posx>648.5</posx>
      <posy>7</posy>
      <width>165</width>
      <height>95</height>
      <visible>true</visible>
      <texture>recorded_blue.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>

    <control type="image" id="5">
      <description>Image 5</description>
      <posx>813.5</posx>
      <posy>7</posy>
      <width>149</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 5.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>
</controls>
</window>

Here is the .py script:

import xbmc 
import xbmcgui
import os

#get actioncodes from keymap.xml
ACTION_MOVE_LEFT = 1
ACTION_MOVE_RIGHT = 2
ACTION_MOVE_UP = 3
ACTION_MOVE_DOWN = 4
ACTION_PREVIOUS_MENU = 10
ACTION_BACKSPACE = 110



class MyClass(xbmcgui.WindowXML):
  def onAction(self, action):

    if action == ACTION_BACKSPACE:
         self.close()

    if action == ACTION_PREVIOUS_MENU:
         self.close()

    if action == ACTION_MOVE_LEFT:

         if os.path.exists('Q:\\resources\skins\Default\media\image 4.jpg') == True:
             self.strAction = xbmcgui.ControlLabel(300, 200, 600, 200, '', 'font14', '0xFF00FF00')
             self.addControl(self.strAction)
             self.strAction.setLabel('Image is exist')

Here's the xml parser:

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))

I tried with:

if action == ACTION_MOVE_LEFT:
         filename = 'script-tvguide-mainmenu.xml'
         tree = ET.parse(filename)
         root = tree.getroot()
         controls = root.find('controls')

         for control in controls.findall('control'):
             texture = control.find('texture').text

             if texture == 'tvguide_yellow.png':
                  self.strAction = xbmcgui.ControlLabel(300, 200, 600, 200, '', 'font14', '0xFF00FF00')
                  self.addControl(self.strAction)
                  self.strAction.setLabel('Image is exisit')

I want to know how to do you use to write in python for xml parser include if statement that if I have the image called "Image 2.jpg" which is return as true then I could do something?


Solution

  • 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:

    def find_image2(filename):
        tree = ET.parse(filename)
        root = tree.getroot()
        controls = root.find('controls')
    
        for control in controls.findall('control'):
            texture = control.find('texture')
            if texture and texture.text == 'Image 2.png':
                return control.find('description').text