Not a simple way, no. You could use the class _ElementMap and _AttributeMap properties to extract elements and attributes from an instance and put them into a dictionary.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is a the snippet. It dont look at attributes.
def recursive_to_dict(node):
if node is None:
return node
res = {}
if isinstance(node, simpleTypeDefinition):
return node
for expanded_name, element_declaration in node._ElementMap.items():
element_name = expanded_name.localName()
node_value = getattr(node, element_name)
if element_declaration.isPlural():
res[element_name] = [
recursive_to_dict(plural_node)
for plural_node in node_value
]
else:
res[element_name] = recursive_to_dict(getattr(node, element_name))
return res
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello. Is there simple way to convert generated PYXB objects to dictionaries?
Last edit: Sergei SInitsyn 2017-05-30
Not a simple way, no. You could use the class
_ElementMap
and_AttributeMap
properties to extract elements and attributes from an instance and put them into a dictionary.Thanks for answer. And is there any example how to do that?
No. I've never encountered a situation where I've had to do it.
Here is a the snippet. It dont look at attributes.