C++/Qt读写xml文件

今天介绍C++/Qt如何读写xml文件,xml文件一般用于作为配置文件使用。

C++

C++读写xml文件需要借助第三方来实现,比较好用的有tinyxml2和pugixml,对应的网址链接。

tinyxml2

pugixml

以tinyxml2为例,下载后进行解压可以看到以下文件:

C++/Qt读写xml文件_第1张图片

项目代码中仅仅需要包含tinyxml2.h和tinyxml2.cpp两个文件即可,以读写这个xml为例

C++/Qt读写xml文件_第2张图片

 以下是读函数

void readXml()
{
    tinyxml2::XMLDocument doc;
    if (doc.LoadFile("1.xml") == XML_SUCCESS) {
        XMLElement *data = doc.FirstChildElement("data");
        if (data) {
            XMLElement *profile = data->FirstChildElement("profile");
            if (profile) {
                cout<Attribute("id")<<","<Attribute("group")<FirstChildElement("name")->GetText()<FirstChildElement("dataID")->GetText()<FirstChildElement("domain");
            if (domain) {
                cout<FirstChildElement("name")->GetText()<FirstChildElement("domainID")->GetText()<FirstChildElement("values");
            if (values) {
                XMLElement *valueNode = values->FirstChildElement("value");
                while (valueNode != NULL) {
                    cout<Attribute("name")<<","<Attribute("type")<<","<<
                        valueNode->Attribute("defaultValue")<<","<GetText()<NextSiblingElement();
                }
            }
        }
    }
}

执行之后可以看到打印

C++/Qt读写xml文件_第3张图片

写函数 

void writeXml()
{
    tinyxml2::XMLDocument doc;
    if (doc.LoadFile("1.xml") == XML_SUCCESS) {
        XMLElement *data = doc.FirstChildElement("data");
        if (data) {
            XMLElement *profile = data->FirstChildElement("profile");
            if (profile) {
                profile->SetAttribute("id", "4321");
                profile->SetAttribute("group", "group1");
                profile->FirstChildElement("name")->SetText("user0000001");
                profile->FirstChildElement("dataID")->SetText("QAZ123");
            }
            XMLElement *domain = data->FirstChildElement("domain");
            if (domain) {
                domain->FirstChildElement("name")->SetText("user0000002");
                domain->FirstChildElement("domainID")->SetText("3");
            }
            XMLElement *values = data->FirstChildElement("values");
            if (values) {
                XMLElement *valueNode = values->FirstChildElement("value");
                while (valueNode != NULL) {
                    valueNode->SetAttribute("name", "uuuu1");
                    valueNode->SetAttribute("type", "int32");
                    valueNode->SetAttribute("defaultValue", "666");
                    valueNode = valueNode->NextSiblingElement();
                }
            }
        }
        doc.SaveFile("1.xml");
    }
}

执行后查看对应文件

C++/Qt读写xml文件_第4张图片

可以看到文件已被修改。

Qt

Qt读写xml文件可以使用QDomDocument或者QXmlStreamReader(QXmlStreamWriter),其中前者是获取整个dom树操作,后者是按照读文件一样按行读取。以下以QDomDocument为例。

首先pro文件包含xml模块:

QT       += xml

以及头文件包含:

#include 

同样是读刚刚的xml文件,以下是读函数

void readXml()
{
    QFile file("1.xml");
    if (file.open(QIODevice::ReadOnly)) {
        QDomDocument doc;
        QString errString;
        if (doc.setContent(&file, &errString)) {
            QDomElement data = doc.documentElement();
            QDomElement profile = data.firstChildElement("profile");
            if (!profile.isNull()) {
                qDebug()<

执行后打印

C++/Qt读写xml文件_第5张图片

写函数

void writeXml()
{
    QFile file("1.xml");
    if (file.open(QIODevice::ReadOnly)) {
        QDomDocument doc;
        QString errString;
        if (doc.setContent(&file, &errString)) {
            file.close();
            QDomElement data = doc.documentElement();
            QDomElement profile = data.firstChildElement("profile");
            if (!profile.isNull()) {
                profile.setAttribute("id", "111111");
                profile.setAttribute("group", "g1");
                profile.firstChildElement("name").firstChild().setNodeValue("User000001");
                profile.firstChildElement("dataID").firstChild().setNodeValue("XYZ123");
            }
            QDomNodeList values = data.firstChildElement("values").childNodes();
            for (int i = 0; i < values.size(); ++i) {
                QDomElement value = values.at(i).toElement();
                value.firstChild().setNodeValue("xxxx");
            }
            qDebug()<

注意修改节点值setNodeValue不是直接调用而是.firstChild().setNodeValue执行后可以查看文件已经被修改

C++/Qt读写xml文件_第6张图片

你可能感兴趣的:(c++,xml)