随笔-341  评论-2670  文章-0  trackbacks-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                LinkIncremental="1"
136                GenerateDebugInformation="true"
137                SubSystem="1"
138                OptimizeReferences="2"
139                EnableCOMDATFolding="2"
140                TargetMachine="1"
141            />
142            <Tool
143                Name="VCALinkTool"
144            />
145            <Tool
146                Name="VCManifestTool"
147            />
148            <Tool
149                Name="VCXDCMakeTool"
150            />
151            <Tool
152                Name="VCBscMakeTool"
153            />
154            <Tool
155                Name="VCFxCopTool"
156            />
157            <Tool
158                Name="VCAppVerifierTool"
159            />
160            <Tool
161                Name="VCPostBuildEventTool"
162            />
163        </Configuration>
164    </Configurations>
165    <References>
166    </References>
167    <Files>
168        <Filter
169            Name="Source Files"
170            Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
171            UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
172            >
173            <File
174                RelativePath=".\Main.cpp"
175                >
176            </File>
177        </Filter>
178        <Filter
179            Name="Header Files"
180            Filter="h;hpp;hxx;hm;inl;inc;xsd"
181            UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
182            >
183        </Filter>
184        <Filter
185            Name="Resource Files"
186            Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
187            UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
188            >
189        </Filter>
190        <Filter
191            Name="Vczh Library++"
192            >
193            <Filter
194                Name="Data"
195                >
196                <Filter
197                    Name="Header Files"
198                    >
199                    <File
200                        RelativePath="..\..\..\..\Library\Data\VL_Data.h"
201                        >
202                    </File>
203                    <File
204                        RelativePath="..\..\..\..\Library\Data\VL_Stream.h"
205                        >
206                    </File>
207                    <File
208                        RelativePath="..\..\..\..\Library\Data\VL_System.h"
209                        >
210                    </File>
211                    <File
212                        RelativePath="..\..\..\..\Library\Data\VL_Uniop.h"
213                        >
214                    </File>
215                </Filter>
216                <Filter
217                    Name="Source Files"
218                    >
219                    <File
220                        RelativePath="..\..\..\..\Library\Data\VL_Data.cpp"
221                        >
222                    </File>
223                    <File
224                        RelativePath="..\..\..\..\Library\Data\VL_Stream.cpp"
225                        >
226                    </File>
227                    <File
228                        RelativePath="..\..\..\..\Library\Data\VL_System.cpp"
229                        >
230                    </File>
231                    <File
232                        RelativePath="..\..\..\..\Library\Data\VL_Uniop.cpp"
233                        >
234                    </File>
235                </Filter>
236            </Filter>
237            <Filter
238                Name="Collection"
239                >
240                <Filter
241                    Name="Header Files"
242                    >
243                    <File
244                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_BalanceTree.h"
245                        >
246                    </File>
247                    <File
248                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Basic.h"
249                        >
250                    </File>
251                    <File
252                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Bintree.h"
253                        >
254                    </File>
255                    <File
256                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Chain.h"
257                        >
258                    </File>
259                    <File
260                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_CustomBintree.h"
261                        >
262                    </File>
263                    <File
264                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Event.h"
265                        >
266                    </File>
267                    <File
268                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Iterator.h"
269                        >
270                    </File>
271                    <File
272                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_List.h"
273                        >
274                    </File>
275                    <File
276                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Map.h"
277                        >
278                    </File>
279                    <File
280                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Pool.h"
281                        >
282                    </File>
283                    <File
284                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_SearchTree.h"
285                        >
286                    </File>
287                    <File
288                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_String.h"
289                        >
290                    </File>
291                </Filter>
292                <Filter
293                    Name="Source Files"
294                    >
295                    <File
296                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Basic.cpp"
297                        >
298                    </File>
299                    <File
300                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Iterator.cpp"
301                        >
302                    </File>
303                    <File
304                        RelativePath="..\..\..\..\Library\Data\Data\VL_Data_String.cpp"
305                        >
306                    </File>
307                </Filter>
308            </Filter>
309            <Filter
310                Name="Platform"
311                >
312                <Filter
313                    Name="Header Files"
314                    >
315                    <File
316                        RelativePath="..\..\..\..\Library\Platform\VL_Console.h"
317                        >
318                    </File>
319                </Filter>
320                <Filter
321                    Name="Source Files"
322                    >
323                    <File
324                        RelativePath="..\..\..\..\Library\Platform\VL_Console.cpp"
325                        >
326                    </File>
327                </Filter>
328            </Filter>
329            <Filter
330                Name="XML"
331                >
332                <Filter
333                    Name="Header Files"
334                    >
335                    <File
336                        RelativePath="..\..\..\..\Library\XML\VL_XML.h"
337                        >
338                    </File>
339                    <File
340                        RelativePath="..\..\..\..\Library\XML\VL_XPath.h"
341                        >
342                    </File>
343                </Filter>
344                <Filter
345                    Name="Source Files"
346                    >
347                    <File
348                        RelativePath="..\..\..\..\Library\XML\VL_XML.cpp"
349                        >
350                    </File>
351                    <File
352                        RelativePath="..\..\..\..\Library\XML\VL_XPath.cpp"
353                        >
354                    </File>
355                </Filter>
356            </Filter>
357            <Filter
358                Name="Grammar"
359                >
360                <Filter
361                    Name="Header Files"
362                    >
363                    <File
364                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_Automaton.h"
365                        >
366                    </File>
367                    <File
368                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_Regexp.h"
369                        >
370                    </File>
371                    <File
372                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegFA.h"
373                        >
374                    </File>
375                    <File
376                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegSim.h"
377                        >
378                    </File>
379                    <File
380                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegTools.h"
381                        >
382                    </File>
383                </Filter>
384                <Filter
385                    Name="Source Files"
386                    >
387                    <File
388                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_Regexp.cpp"
389                        >
390                    </File>
391                    <File
392                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegFA.cpp"
393                        >
394                    </File>
395                    <File
396                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegSim.cpp"
397                        >
398                    </File>
399                    <File
400                        RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegTools.cpp"
401                        >
402                    </File>
403                </Filter>
404            </Filter>
405        </Filter>
406    </Files>
407    <Globals>
408    </Globals>
409</VisualStudioProject>
410

    最后是输出的Output.xml:
  1  <?xml version="1.0" encoding="gb2312" standalone="yes" ?> 
  2<result>
  3  <attributes /> 
  4<nodes>
  5<node>
  6  <File RelativePath=".\Main.cpp" /> 
  7  </node>
  8<node>
  9  <File RelativePath="..\..\..\..\Library\Data\VL_Data.h" /> 
 10  </node>
 11<node>
 12  <File RelativePath="..\..\..\..\Library\Data\VL_Stream.h" /> 
 13  </node>
 14<node>
 15  <File RelativePath="..\..\..\..\Library\Data\VL_System.h" /> 
 16  </node>
 17<node>
 18  <File RelativePath="..\..\..\..\Library\Data\VL_Uniop.h" /> 
 19  </node>
 20<node>
 21  <File RelativePath="..\..\..\..\Library\Data\VL_Data.cpp" /> 
 22  </node>
 23<node>
 24  <File RelativePath="..\..\..\..\Library\Data\VL_Stream.cpp" /> 
 25  </node>
 26<node>
 27  <File RelativePath="..\..\..\..\Library\Data\VL_System.cpp" /> 
 28  </node>
 29<node>
 30  <File RelativePath="..\..\..\..\Library\Data\VL_Uniop.cpp" /> 
 31  </node>
 32<node>
 33  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_BalanceTree.h" /> 
 34  </node>
 35<node>
 36  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Basic.h" /> 
 37  </node>
 38<node>
 39  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Bintree.h" /> 
 40  </node>
 41<node>
 42  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Chain.h" /> 
 43  </node>
 44<node>
 45  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_CustomBintree.h" /> 
 46  </node>
 47<node>
 48  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Event.h" /> 
 49  </node>
 50<node>
 51  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Iterator.h" /> 
 52  </node>
 53<node>
 54  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_List.h" /> 
 55  </node>
 56<node>
 57  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Map.h" /> 
 58  </node>
 59<node>
 60  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Pool.h" /> 
 61  </node>
 62<node>
 63  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_SearchTree.h" /> 
 64  </node>
 65<node>
 66  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_String.h" /> 
 67  </node>
 68<node>
 69  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Basic.cpp" /> 
 70  </node>
 71<node>
 72  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_Iterator.cpp" /> 
 73  </node>
 74<node>
 75  <File RelativePath="..\..\..\..\Library\Data\Data\VL_Data_String.cpp" /> 
 76  </node>
 77<node>
 78  <File RelativePath="..\..\..\..\Library\Platform\VL_Console.h" /> 
 79  </node>
 80<node>
 81  <File RelativePath="..\..\..\..\Library\Platform\VL_Console.cpp" /> 
 82  </node>
 83<node>
 84  <File RelativePath="..\..\..\..\Library\XML\VL_XML.h" /> 
 85  </node>
 86<node>
 87  <File RelativePath="..\..\..\..\Library\XML\VL_XPath.h" /> 
 88  </node>
 89<node>
 90  <File RelativePath="..\..\..\..\Library\XML\VL_XML.cpp" /> 
 91  </node>
 92<node>
 93  <File RelativePath="..\..\..\..\Library\XML\VL_XPath.cpp" /> 
 94  </node>
 95<node>
 96  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_Automaton.h" /> 
 97  </node>
 98<node>
 99  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_Regexp.h" /> 
100  </node>
101<node>
102  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegFA.h" /> 
103  </node>
104<node>
105  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegSim.h" /> 
106  </node>
107<node>
108  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegTools.h" /> 
109  </node>
110<node>
111  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_Regexp.cpp" /> 
112  </node>
113<node>
114  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegFA.cpp" /> 
115  </node>
116<node>
117  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegSim.cpp" /> 
118  </node>
119<node>
120  <File RelativePath="..\..\..\..\Library\Data\Grammar2\VL_RegTools.cpp" /> 
121  </node>
122  </nodes>
123  </result>

    为了方便,输出并没有使用VL_XMLDocument而是直接构造字符串。
posted on 2008-06-27 20:55 陈梓瀚(vczh) 阅读(2396) 评论(2)  编辑 收藏 引用 所属分类: C++

评论:
# re: 使用XPath轻松获得VC9.0工程文件包含的所有代码文件 2008-06-28 06:29 | Lnn
很好很强大  回复  更多评论
  
# re: 使用XPath轻松获得VC9.0工程文件包含的所有代码文件 2008-06-30 20:49 | 企业即时通讯
无语,只想说两个字‘弓虽’。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理