
Sebastian Rahlf, 31.05.2010 14:51:
I have a bit of XML which I want to parse via lxml.objectify.
from lxml import objectify node = objectify.fromstring(''' <Item> <ASIN>0747532745</ASIN> <ItemAttributes> <Author>Joanne K. Rowling</Author> <Manufacturer>Bloomsbury Publishing</Manufacturer> <ProductGroup>Book</ProductGroup> <Title>Harry Potter and the Philosopher's Stone</Title> </ItemAttributes> </Item> ''')
I have the following problem: node.ASIN is evaluated to integer value 747532745 but should be a string ('0747532745').
There is no way for me to influence the incoming XML, so any py:pytype magic or adding a schema is out of the question. Is there a way to ensure that ASIN elements are always evaluated to a string?
Just add the type attribute after parsing and remove it before serialisation. Alternatively, you can register your own Element type for the ASIN tag. There should be something about that in the objectify docs. Stefan