2016-05-19 13 views
0

私はPythonとlxmlでvcxprojを解析しようとしています。私がしようとすると、印刷中に何もないのは、<Project >のものを削除しない限り、何もありません。Pythonとlxmlでvcxprojを解析する

<?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> 

そして、私のpythonコード:私はこのように実行した場合

#!/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) 

、スクリプトがなく、何も表示されない作品

は、ここに私の.vcxproj(私がテストし、それを減少さ)です。ノードDefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"を削除した場合スクリプトの動作...

私はxmlを初めて使っていますが、多分何か間違っています。これを解決するために私を助けることができますか?

ありがとうございました。

答えて

1

見つかりここに解決策:そのようなlxml etree xmlparser remove unwanted namespace

(もしあれば)私は前に正確な名前空間に持っている、と思われる:

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) 
関連する問題