如何用批处理修改XML文件中一行内容

a.xml里有这样一行内容
2023-05-22T20:16:00
如何用批处理将这行内容换成下边这样
2024-03-12T07:16:00

批处理比较难弄,建议是用 powershell,相当于 batch 的高阶版

可以使用C++的标准库中的fstream类,先读入整个XML文件,对需要修改的那一行进行修改,最后再将修改后的内容输出到原文件中。

以下是代码示例:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string filename = "a.xml";
    std::ifstream ifs(filename);
    if (!ifs.is_open()) {
        std::cerr << "Failed to open file " << filename << std::endl;
        return 1;
    }

    std::string line;
    std::string new_line = "    <time>2024-03-12T07:16:00</time>";
    std::string modified_content;

    while (std::getline(ifs, line)) {
        if (line.find("<time>") != std::string::npos) {
            // replace the line with new_line
            line = new_line;
        }
        modified_content += line + "\n";
    }

    ifs.close();
    std::ofstream ofs(filename);
    if (!ofs.is_open()) {
        std::cerr << "Failed to open file " << filename << std::endl;
        return 1;
    }
    ofs << modified_content;
    ofs.close();

    std::cout << "Modification completed!" << std::endl;

    return 0;
}


这里使用了while循环逐行读入整个XML文件,如果某行包含了“

内容一样的吗,替换的话,用下载个notepad文本编辑器,Ctrl+R点击替换,按照输入替换内容,想替换后的内容,左下角勾选拓展,当然也支持正则语法

忘记说了,我需要用DOS命令做一个批处理文件来执行,不是用C++