You are not logged in.

#1 2009-06-18 21:39:42

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Python and XML (rc.conf from openbox) [solved]

Hi All,
i've been trying to get my head round this for a few days now and keep getting stuck.

I'm trying to parse the keybindings from the rc.xml in openbox

the code i have at the moment  (very basic just to try and get it to work)
test.xml is a copy of my rc.xml

  

from xml.dom import minidom

xmldoc = minidom.parse ('test.xml')
reflist = xmldoc.getElementsByTagName('keybind')

a = 0
while a < len(reflist):
        #print reflist[a].toxml()
        a=a+1

bitref = reflist[1]

print bitref.toxml()

the output from this is

<keybind key="W-Right">
      <action name="DesktopRight">
        <dialog>no</dialog>
        <wrap>no</wrap>
      </action>
    </keybind>

what i'm trying to do is pull apart the xml so that i can get the keybind the action and if relevent the command into a sepeart list.

so far i can get the key binding by doing the following

a = bitref.attributes["key"]
print a.name
print a.key

which gives me the following

key
W-Right

if i try

a = bitref.attributes["action"]  
or 
a = bitref.attributes["name"]

i get an error saying invalid key (along those lines)

has any one been able todo this before or point me in the right direction?

Regards
Matthew

Last edited by genisis300 (2009-06-19 01:56:18)


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#2 2009-06-18 23:13:03

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: Python and XML (rc.conf from openbox) [solved]

You're mixing up elements and attributes. Let's break down the following as an example:

<keybind key="W-Right">
      <action name="DesktopRight">
        <dialog>no</dialog>
        <wrap>no</wrap>
      </action>
    </keybind>

This is an element with the tag name "keybind". The keybind element has an attribute named "key" with a value of "W-Right".
Looking at your code,

reflist = xmldoc.getElementsByTagName('keybind')
...
bitref = reflist[1]

you see that you use the getElementsByTagName to retrieve a list of all elements in your document with the tag name "keybind". You then assign the second element in the list to bitref, which is represented by the xml snippet posted above. Note that the list index starts at 0 and that your while loop doesn't actually change anything.

To access an attribute of the "keybind" element, you've used this code (corrected to change "a.key" to "a.value")

a = bitref.attributes["key"]
print a.name
print a.value

The reason that

a = bitref.attributes["action"]

doesn't work is that "<action ...>" is not an attribute of the keybind element but an element itself. You need to use the same method to get this element that you used to get the keybind element, namely ."getElementsByName()". An example should help to clear this up:

#!/usr/bin/env python
from xml.dom import minidom

# load the document
xmldoc = minidom.parse ('test.xml')
# get a list of all the keybinds
keybinds = xmldoc.getElementsByTagName('keybind')

# loop through the keybinds
for keybind in keybinds:

  # print out the xml so we can compare it with the rest of the output
  # this is just for the sake of example
  print keybind.toxml()

  # get the key attribute of the keybind element
  keyAttribute = keybind.attributes["key"]

  # display the name and value of the attribute
  # the name is in keyAttribute.name
  # the value is in keyAttribute.value
  print "\nkeybind attribute: name is %s, value is %s" % (keyAttribute.name,keyAttribute.value)

  # now we want to get the action element
  # this will be the first (and only) item in the list of elements with the tag name "action"
  # note the "[0]" on the end to get the first element
  actionElement = keybind.getElementsByTagName('action')[0]

  # now we want the "name" attribute of this element which shows us which action it is
  actionElementAttribute = actionElement.attributes["name"]

  # as with the key attribute above,
  # the name is in actionElementAttribute.name and
  # the value is in actionElementAttribute.value
  print "\taction element attribute: name is %s, value is %s\n" % (actionElementAttribute.name,actionElementAttribute.value)

Build on that to access the inner elements of the action element and their attributes.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#3 2009-06-19 01:29:27

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: Python and XML (rc.conf from openbox) [solved]

thank you i knew i was missing something.

Again thanks smile


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

Board footer

Powered by FluxBB