Parse the namespace based XML using Python

In this blog, I am considering how to parser and modify the xml file using python. For example, I need to parser the following xml1 (slightly modified for this blog) and need to write the modified xml to out.xml file.

Here the country.xml

<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com"
        xmlns="http://people.example.com">
    <actor type='T1'>
        <name>John Cleese</name>
        <fictional:character>Lancelot</fictional:character>
        <fictional:character>Archie Leach</fictional:character>
    </actor>
    <actor type='T2'>
        <name>Eric Idle</name>
        <fictional:character>Sir Robin</fictional:character>
        <fictional:character>Gunther</fictional:character>
        <fictional:character>Commander Clement</fictional:character>
    </actor>
</actors>

In the above xml there is default namespace as well as fictional namespace.

You have to define that as shwon in the line# 2 - 5, but you cannot have anonymous namesapces.

import xml.etree.ElementTree as ET
ns = {
    'ns'         : 'http://people.example.com',
    'fictional': 'http://characters.example.com'
}


tree = ET.parse('country.xml')
for prefix, uri in ns.items():
    ET.register_namespace(prefix, uri)

root = tree.getroot()
for actor in root.findall("ns:actor[@type='T2']",ns):
    name = actor.find('ns:name',ns)
    name.text ="test"

    for char in actor.findall('fictional:character',ns):
        print(' |-->', char.text)

tree.write("out.xml")

Namespaces are registered in the line#9. The changes to the existing name has been done in the line# 15. Here the output of above

<ns:actors xmlns:fictional="http://characters.example.com" xmlns:ns="http://people.example.com">
    <ns:actor type="T1">
        <ns:name>John Cleese</ns:name>
        <fictional:character>Lancelot</fictional:character>
        <fictional:character>Archie Leach</fictional:character>
    </ns:actor>
    <ns:actor type="T2">
        <ns:name>test</ns:name>
        <fictional:character>Sir Robin</fictional:character>
        <fictional:character>Gunther</fictional:character>
        <fictional:character>Commander Clement</fictional:character>
    </ns:actor>
</ns:actors>

You can see the changes to the name where the element fulfilled the condition of ns:actor[@type='T2'] in the line# 8.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases