如何在不使用 C/C++ (Windows) 中的 DOM 的情况下检查 XML 文件中的标记
How to check for a tag in an XML File without using DOM in C / C++ (Windows)?
我想读取 system32\Tasks 文件夹中的 XML 文件并检查文件中是否存在标签。我只需要检查 XML 文件中的标签。而且该文件不一定必须与项目文件位于同一目录中。 void listdirs(WCHAR *dir, WCHAR *mask) { WCHAR fspec1[1000], fname[1000]; WIN32_FIND_DATAW DTA = {0}; 句柄 hDta = 空; DWORD dLastError = 0; LPCWSTR fspec = NULL;
swprintf(fspec1, 100, L"%ws%ws", dir, mask);
fspec = reinterpret_cast<LPCWSTR>(fspec1);
if ((hDta = FindFirstFileW(fspec, &dta)) == INVALID_HANDLE_VALUE) {
dLastError = GetLastError();
printf("The error : %sn", strerror(dLastError));
}
else {
do {
if ((dta.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
if(wcscmp(dta.cFileName, L".") !=0 && wcscmp(dta.cFileName, L"..") != 0)
{
//Insert code snippet here to check whether the file has <Date> tag
or not
swprintf(fname, 100, L"%ws%ws%ws", dir, dta.cFileName, L"\");
listdirs(fname, mask);
}
}
else
{
count++;
printf("%wsn", dta.cFileName);
printf("Count is : %dn", count);
}
} while (FindNextFileW(hDta, &dta));
FindClose(hDta);
}
}
我尝试使用TinyXml,但我很难理解它,因为我对DOM一无所知,并且更喜欢更简单的选择
如果您只检查标签,则可以使用正则表达式:
#include <iostream>
#include <string>
#include <regex>
bool TagExists(const std::string& input,const std::string& tag)
{
std::regex rgx(std::format("<{}>", tag));
std::smatch match;
if (std::regex_search(input.begin(), input.end(), match, rgx))
return true;
else
return false;
}
但不要忘记,这并不控制 XML 是否有效或标记是否已关闭。它只检查正则表达式。
如果我将其注入到您的代码中,它将是这样的:
swprintf(fspec1, 100, L"%ws%ws", dir, mask);
fspec = reinterpret_cast<LPCWSTR>(fspec1);
if ((hDta = FindFirstFileW(fspec, &dta)) == INVALID_HANDLE_VALUE) {
dLastError = GetLastError();
printf("The error : %sn", strerror(dLastError));
}
else {
do {
if ((dta.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
if(wcscmp(dta.cFileName, L".") !=0 && wcscmp(dta.cFileName, L"..") != 0)
{
//Insert code snippet here to check whether the file has <Date> tag
or not
swprintf(fname, 100, L"%ws%ws%ws", dir, dta.cFileName, L"\");
listdirs(fname, mask);
std::ifstream t(fname);
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
bool st=TagExists(str, "Date");
// make whatever you want with this information. I just printed
printf("Tag "Date" is %s exists in %wsn", st? "":"not", dta.cFileName);
}
}
else
{
count++;
printf("%wsn", dta.cFileName);
printf("Count is : %dn", count);
}
} while (FindNextFileW(hDta, &dta));
FindClose(hDta);
}
}
相关文章:
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在未初始化映射的情况下,将值插入到映射的映射中
- 是默认情况下分配给char数组常量的值
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在已经使用Git的情况下减少编译时间
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何在没有信号的情况下从C++执行QML插槽
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 为什么在某些情况下不写入此文件?
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 在C++中如何在没有pow的情况下进行基础计算
- 松弛原子与无同步情况下的记忆连贯性
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 如何在不使用 C/C++ (Windows) 中的 DOM 的情况下检查 XML 文件中的标记