Skip to content Skip to sidebar Skip to footer

Python Reading Xml

I am newbie on Python programming. I have requirement where I need to read the xml structure and build the new soap request xml by adding namespace like here is the example what I

Solution 1:

One option is to use lxml to iterate over all of the elements and add the namespace uri to the .tag property.

You can use register_namespace() to bind the uri to the desired prefix.

Example...

from lxml import etree

tree = etree.parse("input.xml")

etree.register_namespace("soa", "https://www.w3schools.com/furniture")

for elem in tree.iter():
    elem.tag = f"{{https://www.w3schools.com/furniture}}{elem.tag}"print(etree.tostring(tree, pretty_print=True).decode())

Printed output...

<soa:fooxmlns:soa="https://www.w3schools.com/furniture"><soa:bar><soa:typefoobar="1"/><soa:typefoobar="2"/></soa:bar></soa:foo>

Post a Comment for "Python Reading Xml"