Search code examples
pythonxmlcelementtree

Take 2 XML elements and merge into 1 new element - Python


I am currently working on a project using OpenStreetMap XML documents. Part of the project is to verify some of the data and it's consistency. I am fairly new with Python and with working with XML files so I have really no idea where to start.

Here is a snippet of my XML document:

  <way id="11005330" version="2" timestamp="2013-02-05T20:56:45Z" changeset="14926577" uid="451693" user="bot-mode">
<nd ref="98006629"/>
<nd ref="98006630"/>
<nd ref="98006631"/>
<tag k="highway" v="residential"/>
<tag k="name" v="Kiwi Court"/>
<tag k="tiger:cfcc" v="A41"/>
<tag k="tiger:county" v="Lake, FL"/>
<tag k="tiger:name_base" v="Kiwi"/>
<tag k="tiger:name_type" v="Ct"/>
<tag k="tiger:reviewed" v="no"/>
<tag k="tiger:zip_left" v="34714"/>
<tag k="tiger:zip_right" v="34714"/>

What I want to do now is take the:

    <tag k="tiger:name_base" v="Kiwi"/>
    <tag k="tiger:name_type" v="Ct"/>

and combine them into one new tag:

<tag k="addr:street" v="Kiwi Ct"/>

the other thing is that not all of these have both the name_base and name_type. so for those I just want to create the addr:street tag.

This is an extremely large file so it would have to look through each one and create it. After it creates the new tag I will then need to go ahead and remove the element.

I am using the:import xml.etree.cElementTree as ET

EDIT

I was able to fix part of my problem

    root = tree.getroot()
    for way in root.findall(".//way"):
        kbool = False
        tbool = False
        for key in way.iterfind(".//tag"):
            if key.attrib['k'] == "tiger:name_base":
                kbool = True
                # print(key.attrib['v'])
                base = key.attrib['v']
            if key.attrib['k'] == "tiger:name_type":
                tbool = True
                ttype = key.attrib['v']
        if kbool == True and tbool == True:
            ET.SubElement(way, 'tag k="addr:street" v="{} {}"'.format(base, ttype))
        elif kbool == True and tbool == False:
            ET.SubElement(way, 'tag k="addr:street" v="{}"'.format(base))


tree.write('maps')

The issue I'm having now is that it is writing the address attribut even for ways that do not have the tiger:name_base key.


Solution

  • Using ElementTree.

    XML = """<root>
        <way id="11005330" version="2" timestamp="2013-02-05T20:56:45Z" changeset="14926577" uid="451693" user="bot-mode">
            <nd ref="98006629"/>
            <nd ref="98006630"/>
            <nd ref="98006631"/>
            <tag k="highway" v="residential"/>
            <tag k="name" v="Kiwi Court"/>
            <tag k="tiger:cfcc" v="A41"/>
            <tag k="tiger:county" v="Lake, FL"/>
            <tag k="tiger:name_base" v="Kiwi"/>
            <tag k="tiger:name_type" v="Ct"/>
            <tag k="tiger:reviewed" v="no"/>
            <tag k="tiger:zip_left" v="34714"/>
            <tag k="tiger:zip_right" v="34714"/>
        </way>
    </root>"""
    

    Demo:

    import xml.etree.ElementTree as ET
    
    tree = ET.parse(filename)
    doc = tree.getroot()
    for way in doc.findall(".//way"):            #Find all way tags
        name_base = way.find('.//tag[@k="tiger:name_base"]').get("v")     #Get tiger:name_base attr
        way.remove(way.find('.//tag[@k="tiger:name_base"]'))              #Remove Tag
        name_type = way.find('.//tag[@k="tiger:name_type"]').get("v")     #Get tiger:name_type attr
        way.remove(way.find('.//tag[@k="tiger:name_type"]'))              #Remove Tag
        newNode = ET.SubElement(way, '''tag k="addr:street" v="{} {}"'''.format(name_base, name_type))    #Add New Tag
    tree.write(r"C:\Users\Rakesh\Desktop\testFiles\A2.xml")               #Write to file
    

    Output:

    <root>
        <way changeset="14926577" id="11005330" timestamp="2013-02-05T20:56:45Z" uid="451693" user="bot-mode" version="2">
            <nd ref="98006629" />
            <nd ref="98006630" />
            <nd ref="98006631" />
            <tag k="highway" v="residential" />
            <tag k="name" v="Kiwi Court" />
            <tag k="tiger:cfcc" v="A41" />
            <tag k="tiger:county" v="Lake, FL" />
            <tag k="tiger:reviewed" v="no" />
            <tag k="tiger:zip_left" v="34714" />
            <tag k="tiger:zip_right" v="34714" />
        <tag k="addr:street" v="Kiwi Ct" /></way>
    </root>