I have the following XML that I am trying to recreate with the lxml.objectify package
<file>
<customers>
<customer>
<phone>
<type>home</type>
<number>555-555-5555</number>
</phone>
<phone>
<type>cell</type>
<number>999-999-9999</number>
</phone>
<phone>
<type>home</type>
<number>111-111-1111</number>
</phone>
</customer>
</customers>
</file>
I can’t figure out how to create the phone element multiple times. Basically, I have the following non-working code:
# create phone element 1
root.customers.customer.phone = ""
root.customers.customer.phone.type = data_dict['PRIMARY PHONE1']
root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 1']
# create phone element 2
root.customers.customer.phone = ""
root.customers.customer.phone.type = data_dict['PRIMARY PHONE2']
root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 2']
# create phone element 3
root.customers.customer.phone = ""
root.customers.customer.phone.type = data_dict['PRIMARY PHONE3']
root.customers.customer.phone.number = data_dict['PRIMARY PHONE TYPE 3']
Which of course only outputs one section of phone information in the resulting XML. Does anyone have any ideas?
You should create
objectify.Elementobjects, and add them as children ofroot.customers.For example, inserting two phone numbers can be done like this:
If you get unnecessary attributes on these elements when transforming the xml back to a string, use
objectify.deannotate(root, xsi_nil=True, cleanup_namespaces=True).See lxml’s objectify documentation for exact parameters of
objectify.deannotate.(If you’re using an older version of lxml, which doesn’t include the
cleanup_namespaceskeyword argument, do this instead:)