Search code examples
pythonxmllxmllxml.objectify

Change int element to str element in lxml objectify python


Hope someone can help.

I am using lxml objectify to parse xml, which is returned from a third party integration using objectify.fromstring(). I have one element in my xml which sometimes consists of ints 0-9 only, when it is ints only the leading zero(s) are removed. As there is no set number of how many digits will be returned and the fact sometimes it may have 2 letters before the number, padding the value with zeros wouldn't suffice.

Is there a way I could specify lxml objectify to force the type to a string before doing objectify.fromstring() so it retains the value as received in the xml?

I have had a look at the lxml website but can't seem to find what I am looking for.

Many Thanks


Solution

  • You might also be experiencing this kind of behavior:

    >>> from lxml import objectify
    >>> 
    >>> xml = "<a><b>01</b></a>"
    >>> a = objectify.fromstring(xml)
    >>> print(a.b)
    1
    >>> print(a.b.text)
    01
    

    As you can see, if you get .text property you would get the text of the node as is.

    FYI, created a follow-up topic: lxml.objectify and leading zeros.