1. 程式人生 > >使用XPath輕鬆獲得VC9.0工程檔案包含的所有程式碼檔案

使用XPath輕鬆獲得VC9.0工程檔案包含的所有程式碼檔案

    玩了幾天空之軌跡 FC,終於快結束了。距離最後一門考試還有3天,於是把整個XPath的程式碼寫完了。於是試用一下。

    現在展示一下我那個XPath的使用方法:
1     VL_List<VL_XMLNode* , true> Result;
2     VL_XPath XPath(L"//Files//File[contains(@RelativePath,\".cpp\") or contains(@RelativePath,\".h\")]");
3     XPath.Query(Doc.GetRootElement(),Result);     第一行宣告一個XML節點列表,第二行構造一個XPath,第三行將XPath作用在一個XML文件的根節點上。這個XPath用於把VC++9.0的工程檔案中包含的所有.h檔案和.cpp檔案提取出來。雖然Files裡面包含若干Filter,每一個Filter包含一些Filter和File,不過輸出結果Output.xml把所有的滿足要求的File節點都找了出來。

    下面是完整的程式碼,包含輸入輸出、一份vcproj檔案和一份結果的XML:
    首先是C++程式碼:

#include

"..\..\..\..\Library\Platform\VL_Console.h"

#include "..\..\..\..\Library\Data\VL_System.h"

#include "..\..\..\..\Library\Data\VL_Stream.h"

#include "..\..\..\..\Library\XML\VL_XML.h"

using namespace vl;

using namespace vl::platform;

using namespace vl::system;

using namespace vl::stream;

using

namespace vl::xml;

void vlmain(VL_Console& Con)

{

Con.SetPauseOnExit(false);

Con.SetTestMemoryLeaks(true);

Con.SetTitle(L"Vczh XML");

/*設定輸入輸出檔案*/

VUnicodeString AppPath=VFileName(Con.GetAppPath()).MakeAbsolute(L"..\\").GetStrW();

VUnicodeString InputFile=AppPath+L"Input.xml";

VUnicodeString OutputFile=AppPath+L"Output.xml"

;

/*載入Input.xml*/

VL_XMLDocument Doc;

{

VL_FileInputStream Stream(InputFile);

Doc.Load(&Stream);

}

/*使用XPath對根節點進行搜尋*/

VL_List<VL_XMLNode* , true> Result;

VL_XPath XPath(L"//Files//File[contains(@RelativePath,\".cpp\") or contains(@RelativePath,\".h\")]");

XPath.Query(Doc.GetRootElement(),Result);

/*將搜尋到的attribute和其他節點分開*/

VUnicodeString OutputAttributes;

VUnicodeString OutputNodes;

for(VInt i=0;i<Result.GetCount();i++)

{

if(Result[i]->GetAttribute())

{

OutputAttributes+=L"<attribute "+Result[i]->GetXMLText()+L"/>";

}

else

{

OutputNodes+=L"<node>"+Result[i]->GetXMLText()+L"</node>";

}

}

/*將結果儲存到Output.xml*/

VL_TextOutput(new VL_FileOutputStream(OutputFile,false),true,vceMbcs,true).Write

(L"<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"yes\"?><result><attributes>"

+OutputAttributes

+L"</attributes><nodes>"

+OutputNodes

+L"</nodes></result>"

);

}


    其次是vcproj檔案(Input.xml):
  1<?xml version="1.0" encoding="gb2312"?>  2<VisualStudioProject
  3    ProjectType="Visual C++"  4    Version="9.00"  5    Name="XMLParser"  6    ProjectGUID="{55115CF4-85E7-4647-BD31-62A5CEFB450C}"  7    RootNamespace="XMLParser"  8    Keyword="Win32Proj"  9    TargetFrameworkVersion="131072" 10    > 11    <Platforms> 12        <Platform
 13            Name="Win32" 14        /> 15    </Platforms> 16    <ToolFiles> 17    </ToolFiles> 18    <Configurations> 19        <Configuration
 20            Name="Debug|Win32" 21            OutputDirectory="$(SolutionDir)$(ConfigurationName)" 22            IntermediateDirectory="$(ConfigurationName)" 23            ConfigurationType="1" 24            CharacterSet="1" 25            > 26            <Tool
 27                Name="VCPreBuildEventTool" 28            /> 29            <Tool
 30                Name="VCCustomBuildTool" 31            /> 32            <Tool
 33                Name="VCXMLDataGeneratorTool" 34            /> 35            <Tool
 36                Name="VCWebServiceProxyGeneratorTool" 37            /> 38            <Tool
 39                Name="VCMIDLTool" 40            /> 41            <Tool
 42                Name="VCCLCompilerTool" 43                Optimization="0" 44                PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" 45                MinimalRebuild="true" 46                BasicRuntimeChecks="3" 47                RuntimeLibrary="1" 48                UsePrecompiledHeader="0" 49                WarningLevel="3" 50                DebugInformationFormat="4" 51            /> 52            <Tool
 53                Name="VCManagedResourceCompilerTool" 54            /> 55            <Tool
 56                Name="VCResourceCompilerTool" 57            /> 58            <Tool
 59                Name="VCPreLinkEventTool" 60            /> 61            <Tool
 62                Name="VCLinkerTool" 63                LinkIncremental="2" 64                GenerateDebugInformation="true" 65                SubSystem="1" 66                TargetMachine="1" 67            /> 68            <Tool
 69                Name="VCALinkTool" 70            /> 71            <Tool
 72                Name="VCManifestTool" 73            /> 74            <Tool
 75                Name="VCXDCMakeTool" 76            /> 77            <Tool
 78                Name="VCBscMakeTool" 79            /> 80            <Tool
 81                Name="VCFxCopTool" 82            /> 83            <Tool
 84                Name="VCAppVerifierTool" 85            /> 86            <Tool
 87                Name="VCPostBuildEventTool" 88            /> 89        </Configuration> 90        <Configuration
 91            Name="Release|Win32" 92            OutputDirectory="$(SolutionDir)$(ConfigurationName)" 93            IntermediateDirectory="$(ConfigurationName)" 94            ConfigurationType="1" 95            CharacterSet="1" 96            WholeProgramOptimization="1" 97            > 98            <Tool
 99                Name="VCPreBuildEventTool"100            />101            <Tool
102                Name="VCCustomBuildTool"103            />104            <Tool
105                Name="VCXMLDataGeneratorTool"106            />107            <Tool
108                Name="VCWebServiceProxyGeneratorTool"109            />110            <Tool
111                Name="VCMIDLTool"112            />113            <Tool
114                Name="VCCLCompilerTool"115                Optimization="2"116                EnableIntrinsicFunctions="true"117                PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"118                RuntimeLibrary="0"119                EnableFunctionLevelLinking="true"120                UsePrecompiledHeader="0"121                WarningLevel="3"122                DebugInformationFormat="3"123            />124            <Tool
125                Name="VCManagedResourceCompilerTool"126            />127            <Tool
128                Name="VCResourceCompilerTool"129            />130            <Tool
131                Name="VCPreLinkEventTool"132            />133            <Tool
134                Name="VCLinkerTool"135                LinkInc