pythonxmlvcxproj

Parsing vcxproj with python and lxml


I'm trying to parse a vcxproj with Python and lxml. When I attempt to do that nothing is chown during print unless I delete what's in <Project >.

Here is my .vcxproj (I reduced it to test) :

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|x64">
      <Configuration>Debug</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="ReleaseDebug|Win32">
      <Configuration>ReleaseDebug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="ReleaseDebug|x64">
      <Configuration>ReleaseDebug</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|x64">
      <Configuration>Release</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
  </ItemGroup>
</Project>

And my python code :

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from lxml import etree

tree = etree.parse("core.xml")

for conf in tree.xpath("/Project/ItemGroup/ProjectConfiguration/Configuration"):
    print(conf.text)

If I run like this, script works but show nothing. If I delete DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" in node Project script works...

I'm new to xml, maybe I'm doing something wrong. can you help me please to solve this ?

Thanks for help.


Solution

  • Found solution here : lxml etree xmlparser remove unwanted namespace

    It seems, I have to precise namespace before (if there is one) like that :

    from lxml import etree
    
    tree = etree.parse("core.xml")
    
    namespaces = {'ns':'http://schemas.microsoft.com/developer/msbuild/2003'}
    for conf in tree.xpath('//ns:Configuration', namespaces=namespaces):
        print (conf.text)