0

I am trying to use ElementTree to parse information out of a .targets file from a NuGet package. What I am trying to find is the tag <AdditionalIncludeDirectories>. I am using the following python code to try to get it:

import xml.etree.ElementTree as ET

tree =  ET.parse(targets_file_path)
config_root = tree.getroot()
namespace = config_root.tag.replace("Project", "")
to_find = f"{namespace}AdditionalIncludeDirectories"
include_dirs = config_root.findall(to_find)
include_directory_list = []
for p in include_dirs:
    include_directory_list.append(p)

This is essentially the first example from the python documentation. The list include_directory_list, however is empty after I execute this snippet.

When I print every single tag, however, I get the following output:

depth = 0
item_list = []
for it in config_root:
    item_list.append([depth, it])
    
while len(item_list) > 0:
    d, it = item_list.pop(0)
    print(f"depth {d}: {it.tag}")
    for subit in it:
        item_list.append([d+1, subit])

yields:

depth 0: {http://schemas.microsoft.com/developer/msbuild/2003}PropertyGroup
depth 0: {http://schemas.microsoft.com/developer/msbuild/2003}ItemDefinitionGroup
depth 0: {http://schemas.microsoft.com/developer/msbuild/2003}ItemDefinitionGroup
depth 1: {http://schemas.microsoft.com/developer/msbuild/2003}ClCompile
depth 1: {http://schemas.microsoft.com/developer/msbuild/2003}Link
depth 1: {http://schemas.microsoft.com/developer/msbuild/2003}Link
depth 2: {http://schemas.microsoft.com/developer/msbuild/2003}AdditionalIncludeDirectories
depth 2: {http://schemas.microsoft.com/developer/msbuild/2003}AdditionalDependencies
depth 2: {http://schemas.microsoft.com/developer/msbuild/2003}AdditionalLibraryDirectorie
depth 2: {http://schemas.microsoft.com/developer/msbuild/2003}AdditionalDependencies
depth 2: {http://schemas.microsoft.com/developer/msbuild/2003}AdditionalLibraryDirectories

Which is exactly the output I expected. The first item with depth=2 is the one I am looking for and the name is seemingly the one I am inputting to findall(). I tried iterfind() as well, but that also does not give me the correct output. There is also the option of passing a namespace dictionary, which I have also tried with the same result. I have the feeling that I am either seeing a bug or I am missing something very very obvious.

Any help would be greatly appreciated!


PS: The XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemDefinitionGroup>
        <ClCompile>
            <AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)native\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup>
        <Link Condition="'$(Configuration)'=='Debug'">
            <AdditionalDependencies>mylib_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
            <AdditionalLibraryDirectories>$(MSBuildThisFileDirectory)native\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
        </Link>
        <Link Condition="'$(Configuration)'=='Release'">
            <AdditionalDependencies>mylib.lib;%(AdditionalDependencies)</AdditionalDependencies>
            <AdditionalLibraryDirectories>$(MSBuildThisFileDirectory)native\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
        </Link>
    </ItemDefinitionGroup>
</Project>

1 Answer 1

0

You have to define the namespace, than you can search with find():

import xml.etree.ElementTree as ET

root = ET.parse("YourFile.xml").getroot()

ns = {'': 'http://schemas.microsoft.com/developer/msbuild/2003'}
print(root.find(".//AdditionalIncludeDirectories", ns).text)

Output:

$(MSBuildThisFileDirectory)native\include\;%(AdditionalIncludeDirectories)
1
  • So the only problem I had was that I did not add ".//" in front of the path I wanted to find. It works even without specifying the namespace if I do that.. Thanks! It seems it was the very very obvious problem, not a bug :D
    – meetaig
    Commented Jul 3 at 10:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.