Python code for Adding tag in xml where parent tag is multiple with different attributes
Question: I'm parsing the following XML file using `xml.etree.ElementTree`:

    
    
    <main>
        <stream id="1" name="some">
            <inner id="500">
                  <sub-inner>
                     <inside> 500 </inside>
                  <sub-inner>
            <inner>
        <stream id="2" name="some">
            <inner id="500">
                  <sub-inner>
                     <inside> 500 </inside>
                  <sub-inner>
            <inner>
        </stream>
    </main>
    

How do I insert `<outer>200</outer>` element into the < sub-inner> tag where
stream id ="2" one?


Answer: 
    import xml.etree.ElementTree as ET
    
    root = ET.fromstring('''
    <main>
        <stream id="1" name="some">
            <inner>500</inner>
        </stream>
        <stream id="2" name="some">
            <inner>500</inner>
        </stream>
    </main>''')
    stream = root.find('.//stream[@id="2"]')
    outer = ET.SubElement(stream, 'outer')
    outer.text = '200'
    print(ET.tostring(root))
    

output:

    
    
    <main>
        <stream id="1" name="some">
            <inner>500</inner>
        </stream>
        <stream id="2" name="some">
            <inner>500</inner>
        <outer>200</outer></stream>
    </main>
    

* * *

If you want `outer` to come before the `inner`:

    
    
    ...
    stream = root.find('.//stream[@id="2"]')
    outer = ET.Element('outer')
    outer.text = '200'
    stream.insert(0, outer)
    print(ET.tostring(root))
    

output:

    
    
    <main>
        <stream id="1" name="some">
            <inner>500</inner>
        </stream>
        <stream id="2" name="some">
            <outer>200</outer><inner>500</inner>
        </stream>
    </main>
    

