专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »C 教程 » vc9.0:使用XPath轻松获得VC9.0工程文件包含的所有代码文件 »正文

vc9.0:使用XPath轻松获得VC9.0工程文件包含的所有代码文件

来源: 发布时间:星期四, 2008年9月25日 浏览:83次 评论: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"

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: