在 C++ 中读写 XML 文件

发布于 2024-10-26 12:34:44 字数 1436 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-11-02 12:34:44

我推荐 MSXML。它可能看起来很复杂,但是当您习惯它时,它会很好而且很容易。
这是一个示例:

input.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Car>
    <Wheels>
        <Wheel1>FL</Wheel1>
        <Wheel2>FR</Wheel2>
        <Wheel3>RL</Wheel3>
        <Wheel4>RR</Wheel4>
    </Wheels>
</Car>

代码:

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll> rename_namespace(_T("MSXML"))

int main(int argc, char* argv[]) {
    HRESULT hr = CoInitialize(NULL); 
    if (SUCCEEDED(hr)) {
        try {
            MSXML::IXMLDOMDocument2Ptr xmlDoc;
            hr = xmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument60),
                                       NULL, CLSCTX_INPROC_SERVER);
            // TODO: if (FAILED(hr))...

            if (xmlDoc->load(_T("input.xml")) != VARIANT_TRUE) {
                printf("Unable to load input.xml\n");
            } else {
                printf("XML was successfully loaded\n");

                xmlDoc->setProperty("SelectionLanguage", "XPath");
                MSXML::IXMLDOMNodeListPtr wheels = xmlDoc->selectNodes("/Car/Wheels/*");
                printf("Car has %u wheels\n", wheels->Getlength());

                MSXML::IXMLDOMNodePtr node;
                node = xmlDoc->createNode(MSXML::NODE_ELEMENT, _T("Engine"), _T(""));
                node->text = _T("Engine 1.0");
                xmlDoc->documentElement->appendChild(node);
                hr = xmlDoc->save(_T("output.xml"));
                if (SUCCEEDED(hr))
                    printf("output.xml successfully saved\n");
            }
        } catch (_com_error &e) {
            printf("ERROR: %ws\n", e.ErrorMessage());
        }
        CoUninitialize();
    }
    return 0;
}

输出:
XML 已成功加载
汽车有 4 个轮子
output.xml 已成功保存

output.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Car>
    <Wheels>
        <WheelLF>1</WheelLF>
        <WheelRF>2</WheelRF>
        <WheelLR>3</WheelLR>
        <WheelRR>4</WheelRR>
    </Wheels>
    <Engine>Engine 1.0</Engine></Car>

您将在这里找到所需的一切:
http://msdn.microsoft.com/en- us/library/ms765540(v=vs.85).aspx

希望有人觉得这很有用;)

I recommend MSXML. It may look complicated, but it's nice and easy when you get used to it.
Here's a sample:

input.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Car>
    <Wheels>
        <Wheel1>FL</Wheel1>
        <Wheel2>FR</Wheel2>
        <Wheel3>RL</Wheel3>
        <Wheel4>RR</Wheel4>
    </Wheels>
</Car>

code:

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll> rename_namespace(_T("MSXML"))

int main(int argc, char* argv[]) {
    HRESULT hr = CoInitialize(NULL); 
    if (SUCCEEDED(hr)) {
        try {
            MSXML::IXMLDOMDocument2Ptr xmlDoc;
            hr = xmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument60),
                                       NULL, CLSCTX_INPROC_SERVER);
            // TODO: if (FAILED(hr))...

            if (xmlDoc->load(_T("input.xml")) != VARIANT_TRUE) {
                printf("Unable to load input.xml\n");
            } else {
                printf("XML was successfully loaded\n");

                xmlDoc->setProperty("SelectionLanguage", "XPath");
                MSXML::IXMLDOMNodeListPtr wheels = xmlDoc->selectNodes("/Car/Wheels/*");
                printf("Car has %u wheels\n", wheels->Getlength());

                MSXML::IXMLDOMNodePtr node;
                node = xmlDoc->createNode(MSXML::NODE_ELEMENT, _T("Engine"), _T(""));
                node->text = _T("Engine 1.0");
                xmlDoc->documentElement->appendChild(node);
                hr = xmlDoc->save(_T("output.xml"));
                if (SUCCEEDED(hr))
                    printf("output.xml successfully saved\n");
            }
        } catch (_com_error &e) {
            printf("ERROR: %ws\n", e.ErrorMessage());
        }
        CoUninitialize();
    }
    return 0;
}

output:
XML was successfully loaded
Car has 4 wheels
output.xml successfully saved

output.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Car>
    <Wheels>
        <WheelLF>1</WheelLF>
        <WheelRF>2</WheelRF>
        <WheelLR>3</WheelLR>
        <WheelRR>4</WheelRR>
    </Wheels>
    <Engine>Engine 1.0</Engine></Car>

You will find everything you need here:
http://msdn.microsoft.com/en-us/library/ms765540(v=vs.85).aspx

Hope someone finds this useful ;)

橙幽之幻 2024-11-02 12:34:44

Boost 序列化器可以做到这一点,如果您将一个对象传递给它,它会写入一个包含所有类属性的文件(二进制或 xml,甚至简单的文本文件)。

Boost serializer can do the trick, if you pass an object to it, it write a file (binary or xml or even a simple text file) with all the class properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文