User
User

Reputation: 24741

Print lxml.objectify.ObjectifiedElement?

Printing a lxml.objectify.ObjectifiedElement just prints a blank line, so I have to access it via it's tags and when I don't know the tags of the response, I'm just guessing.

How do I print the entire object, showing children names and values?

As requested, here is the code I have. Not sure what purpose this holds, but:

from amazonproduct import API
api = API('xxxxx', 'xxxxx', 'us', 'xxxx')

result = api.item_lookup('B00H8U93JO', ResponseGroup='OfferSummary')
print result

Upvotes: 2

Views: 2618

Answers (2)

Cheekysoft
Cheekysoft

Reputation: 35580

In response to har07, You can use minidom to prettify

from lxml import objectify, etree
from xml.dom import minidom

def pretty_print( elem ):
    xml = etree.tostring( elem )
    pretty = minidom.parseString( xml ).toprettyxml( indent='  ' )
    print( pretty )

Upvotes: 0

har07
har07

Reputation: 89295

Using lxml.etree.tostring() seems to work, although not prettified :

>>> from lxml import etree
>>> from lxml import objectify
>>> raw = '''<root>
... <foo>foo</foo>
... <bar>bar</bar>
... </root>'''
... 
>>> root = objectify.fromstring(raw)
>>> print type(root)
<type 'lxml.objectify.ObjectifiedElement'>
>>> print etree.tostring(root)
<root><foo>foo</foo><bar>bar</bar></root>

Upvotes: 6

Related Questions