You didn't tell enough info on how to distinguish ItemGroup
s between each other, but I'm assuming it is based on what kind of nodes are inside.
Here's how you can insert new Folder
tags into the ItemGroup
of Folder
s and None
s. The idea is to find the first Folder
and None
tags, get the parent and appendChild
to it:
import xml.dom.minidom
data = """<Project attri1="build">
<Import Project="blahblah" xmlns="someURI" />
<ItemGroup>
<Folder Include="Scripts" />
<Folder Include="Scripts/1.0" />
<Folder Include="Scripts/2.0/" />
</ItemGroup>
<ItemGroup/>
<ItemGroup>
<None Include="Scripts/1.0/001.sql" />
<None Include="Scripts/2.0/002.sql" />
</ItemGroup>
</Project>"""
dom = xml.dom.minidom.parseString(data)
new_folder = dom.createElement('Folder')
new_folder.setAttribute('Include', 'newpath')
folder = dom.getElementsByTagName('Folder')[0]
folder.parentNode.appendChild(new_folder)
new_script = dom.createElement('Folder')
new_script.setAttribute('Include', 'newsql')
none = dom.getElementsByTagName('None')[0]
none.parentNode.appendChild(new_script)
print dom.toprettyxml()
It prints:
<?xml version="1.0" ?>
<Project attri1="build">
<Import Project="blahblah" xmlns="someURI"/>
<ItemGroup>
<Folder Include="Scripts"/>
<Folder Include="Scripts/1.0"/>
<Folder Include="Scripts/2.0/"/>
<Folder Include="newpath"/>
</ItemGroup>
<ItemGroup/>
<ItemGroup>
<None Include="Scripts/1.0/001.sql"/>
<None Include="Scripts/2.0/002.sql"/>
<Folder Include="newsql"/>
</ItemGroup>
</Project>