Python 3 Script to Convert XML Document to Object in Command Line – Pythongdb Mini Project

In this project we will create a python 3 script to convert XML Document to object in command Line

xml = """<main>
<object1 attr="name">content</object1>
<object1 attr="foo">contenbar</object1>

from lxml import objectify

main = objectify.fromstring(xml)
main.object1[0]             # content
main.object1[1]             # contenbar
main.object1[0].get("attr") # name
main.test                   # me
item = objectify.Element("item")
item.title = "Best of python"
item.price = 17.98
item.price.set("currency", "EUR")

order = objectify.Element("order")
order.item.quantity = 3
order.price = sum(item.price * item.quantity for item in order.item)

import lxml.etree
print(lxml.etree.tostring(order, pretty_print=True))
    <title>Best of python</title>
    <price currency="EUR">17.98</price>

Leave a Comment