2

If I have an ordered list in XML

<Boa>
    <Channels>
        <Channel name="Red"/>
        <Channel name="Green"/>
        <Channel name="Blue" />
    </Channels>
</Boa>

This code

from lxml import objectify

Boa = objectify.parse(self.xml).getroot()

only gets me

Boa.Channels.Channel

with a single entry for Channel.

How do I get this as an ordered list in lxml objectify? I'm also fine with changing my XML markup if there's something that lxml expects to automatically do the conversion.

empty
  • 5,194
  • 3
  • 32
  • 58
  • this might help... https://stackoverflow.com/questions/39390653/getting-contents-of-an-lxml-objectify-comment – Any Moose Aug 17 '18 at 18:27

1 Answers1

3

objectify is a bit weird as it tries to map xml to python objects and this is not 100% match so it has to compromise.

Boa.Channels.Channel is the first Channel

>>> Boa.Channels.Channel.get('name')
'Red'

But at the same time it can also work as a list of Channels:

>>> Boa.Channels.Channel[0].get('name')
'Red'
>>> Boa.Channels.Channel[1].get('name')
'Green'
>>> Boa.Channels.Channel[2].get('name')
'Blue'
>>> [c.get('name') for c in Boa.Channels.Channel]
['Red', 'Green', 'Blue']
nosklo
  • 217,122
  • 57
  • 293
  • 297