-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetree2xml.cpp
120 lines (96 loc) · 6.9 KB
/
filetree2xml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// filetree2xml.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
// xerces includes
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <iostream>
XERCES_CPP_NAMESPACE_USE
void SaveFileTreeToXML(const XMLCh* RootPath, DOMNode* RootNode, XERCES_CPP_NAMESPACE::DOMDocument* Document)
{
WIN32_FIND_DATA FindData;
ZeroMemory( &FindData, sizeof(WIN32_FIND_DATA));
XMLSize_t SearchPatternLen = XMLString::stringLen( RootPath ) + 1 + 1;
XMLCh* SearchPattern = new XMLCh[SearchPatternLen];
ZeroMemory(SearchPattern, sizeof(XMLCh)*SearchPatternLen );
XMLString::catString( SearchPattern, RootPath );
XMLString::catString( SearchPattern, L"*");
HANDLE hSearch = FindFirstFile(SearchPattern, &FindData);
delete[] SearchPattern;
if( INVALID_HANDLE_VALUE == hSearch )
return;
do
{
XMLCh* FileName = FindData.cFileName;
if( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
if( (XMLString::compareIString(FileName,L".")!=0) &&
(XMLString::compareIString(FileName,L"..")!=0) )
{
DOMElement* dir = Document->createElement(L"directory");
dir->setAttribute(L"name", FileName );
RootNode->appendChild(dir);
XMLSize_t SubDirPathLen = XMLString::stringLen( RootPath ) + XMLString::stringLen( FileName ) + 1 + 1;
XMLCh* SubDir = new XMLCh[ SubDirPathLen ];
ZeroMemory( SubDir, sizeof(XMLCh)*SubDirPathLen );
XMLString::catString( SubDir, RootPath );
XMLString::catString( SubDir, FileName);
XMLString::catString( SubDir, L"\\" );
SaveFileTreeToXML( SubDir, dir, Document );
delete[] SubDir;
}
}
else
{
DOMElement* file = Document->createElement(L"file");
file->setAttribute(L"name", FileName );
RootNode->appendChild(file);
}
}while( FindNextFile( hSearch, &FindData) != 0 );
FindClose( hSearch );
}
void SaveXMLToFile(const XMLCh* FileName, XERCES_CPP_NAMESPACE::DOMDocument* Document )
{
}
void PrintXMLdoc(const XERCES_CPP_NAMESPACE::DOMDocument* doc)
{
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(L"Core");
DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
DOMConfiguration* serializerConfig=theSerializer->getDomConfig();
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
XMLFormatTarget *outputStream = new StdOutFormatTarget();
theOutputDesc->setByteStream(outputStream);
theSerializer->write(doc, theOutputDesc);
theOutputDesc->release();
theSerializer->release();
delete outputStream;
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
XMLPlatformUtils::Initialize();
}
catch(...)
{
return 1;
}
XMLCh* Path = NULL;
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(L"Core");
XERCES_CPP_NAMESPACE::DOMDocument* doc = impl->createDocument( 0, L"directory", 0);
if( argc >= 2)
Path = argv[1];
else
Path = L".\\";
SaveFileTreeToXML(Path, doc->getDocumentElement() ,doc);
PrintXMLdoc( doc );
doc->release();
XMLPlatformUtils::Terminate();
return 0;
}