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. -- https://mail.python.org/mailman/listinfo/python-list