Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110178

Re: lxml - SubElement dump root doesn't dump like Element dump does

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: lxml - SubElement dump root doesn't dump like Element dump does
Date 2016-06-20 08:19 +0200
Organization None
Message-ID <mailman.149.1466403557.2288.python-list@python.org> (permalink)
References <8a1bbff2-14a4-4a1a-b53f-ca94f94d54bc@googlegroups.com> <nk81sq$khk$1@ger.gmane.org>

Show all headers | View raw


Sayth Renshaw wrote:

> Afternoon
> 
> Wondering has anyone much experience with lxml specifically objectify?
> 
> When I pick up a file with lxml and use objectify dumping root works as
> expected actually better its quite nice. This is how i do it, file
> handling part left out for brevity.
> 
> def getsMeet(file_list):
>     for filename in sorted(file_list):
>         filename=my_dir + filename
>         yield filename
> 
> def parseXML():
>     """
>     """
>     for file in getsMeet(file_list):
>         with open(file) as f:
>             xml = f.read()
> 
>             root = objectify.fromstring(xml)
>             print(root.tag)
>             print(objectify.dump(root))
>             race = objectify.SubElement(root,"race")
>             print(objectify.dump(race))
> 
> 
> parseXML()
> 
> So the first call to print(objectify.dump(root)) gives as a sample.
> 
> meeting = None [ObjectifiedElement]
>   * id = '42977'
>   * barriertrial = '0'
>   * venue = 'Rosehill Gardens'
>   * date = '2016-05-21T00:00:00'
>   * gearchanges = '-1'
>   * stewardsreport = '-1'
>   * gearlist = '-1'
>   * racebook = '0'
>   * postracestewards = '0'
>   * meetingtype = 'TAB'
>   * rail = 'Timing - Electronic : Rail - +6m'
>   * weather = 'Fine      '
>   * trackcondition = 'Good 3    '
>   * nomsdeadline = '2016-05-16T11:00:00'
>   * weightsdeadline = '2016-05-17T16:00:00'
>   * acceptdeadline = '2016-05-18T09:00:00'
>   * jockeydeadline = '2016-05-18T12:00:00'
>     club = '' [StringElement]
>       * abbrevname = 'Australian Turf Club'
>       * code = '56398'
>       * associationclass = '1'
>       * website = 'http://'
>     race = None [ObjectifiedElement]
>       * id = '215411'
>       * number = '1'
>       * nomnumber = '9'
> 
> Then I am confused when I want to repeat this but only for the subelement
> race I get a return but not as expected.
> 
> This is my return
> race = '' [StringElement]
> 
> so why do i not get all the elements of race as I do when i dump the root?

Because race is a new SubElement that you just created:

>>> help(lxml.objectify.SubElement)
Help on built-in function SubElement in module lxml.etree:

SubElement(...)
    SubElement(_parent, _tag, attrib=None, nsmap=None, **_extra)
    
    Subelement factory.  This function creates an element instance, and
    appends it to an existing element.

>>>

Existing subelements can be accessed as attributes. I'd say that's the very 
point of the lxml.objectify library ;)

For example:

>>> root = 
lxml.objectify.fromstring("<a><b><c>one</c><d>two</d></b><b>second 
b</b></a>")
>>> print(lxml.objectify.dump(root.b))
b = None [ObjectifiedElement]
    c = 'one' [StringElement]
    d = 'two' [StringElement]
>>> print(lxml.objectify.dump(root.b[0]))
b = None [ObjectifiedElement]
    c = 'one' [StringElement]
    d = 'two' [StringElement]
>>> print(lxml.objectify.dump(root.b[1]))
b = 'second b' [StringElement]

> Cheers
> 
> Sayth
> PS I am referring to this documentation
> http://lxml.de/objectify.html#element-access-through-object-attributes

Read that again.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

lxml - SubElement dump root doesn't dump like Element dump does Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-19 20:51 -0700
  Re: lxml - SubElement dump root doesn't dump like Element dump does Peter Otten <__peter__@web.de> - 2016-06-20 08:19 +0200
    Re: lxml - SubElement dump root doesn't dump like Element dump does Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-20 00:13 -0700
    Re: lxml - SubElement dump root doesn't dump like Element dump does Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-20 01:21 -0700

csiph-web