Qt 使用Dom 操作XML的范例

//--------------------读-----------------------------------------------
    QString filename = "D:/zhao/qt/tryxml-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/history.xml";
    qDebug() << filename;
    QFile file(filename);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        qDebug() << "Error: Cannot read file" << filename
                 << ": " << file.errorString();
    }else
    {
        qDebug() << "succeed";
    }
    QString errorStr;
    int errorLine;
    int errorColumn;

    QDomDocument doc;
    if(!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
    {
        qDebug() << "Error: Parse error at line" << errorLine << ", "
                 << "column " << errorColumn << ": "
                 << errorStr;

    }
    QDomElement root = doc.documentElement();
    if(root.tagName() != "history")
    {
        qDebug() << "error";
    }

    QDomNode child = root.firstChild();
    while(!child.isNull())
    {
        if(child.toElement().tagName() == "data")
        {
            QDomNode grandChild = child.firstChild();
            while(!grandChild.isNull())
            {
                qDebug() << grandChild.toElement().tagName() << " " << grandChild.toElement().text();
                grandChild = grandChild.nextSibling();
            }
        }
        child = child.nextSibling();
    }
    //root.appendChild()
    file.close();
//-----------------------结束读------------------------------

//----------------------追加-----------------------------------------
    //QStringList attribs = ""; // 由用户指定
    QDomElement dataNode = doc.createElement("data");
    //carNode.setAttribute("ISBN", isbn); // isbn由用户指定

    //此处开始处理attribs字符串列表中的内容


    QDomText textNode = doc.createTextNode("1989"); // 取出每一个字符串

    QDomElement timeNode = doc.createElement("time");
    //attribNode.setAttribute("number", attribNumber);
    timeNode.appendChild(textNode);

    dataNode.appendChild(timeNode); // 属性结束

    root.appendChild(dataNode);
    //QDomNodeList historyNode = doc.elementsByTagName("history");
    //historyNode.item(0).appendChild(dataNode);

    // 写入XML文件中
    if (file.open(QIODevice::WriteOnly)) {
        QTextStream stream(&file);
        //historyNode.item(0).save(stream, 4);
        doc.save(stream, 4);
        file.close();
    }
//------------------------------结束追加-----------------------------------

//-----------------------------新建------------------------------------------
    QString backupname = "D:/zhao/qt/tryxml-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/backup.xml";
    //qDebug() << filename;
    QFile backup(backupname);
    QDomDocument backupDoc;
    //要先open file
    backup.open(QIODevice::WriteOnly| QFile::Text);

    QDomElement rootNode = backupDoc.createElement("history");
    //QDomText tNode = doc.createTextNode("1989"); // 取出每一个字符串
    //rootNode.appendChild(tNode);
    backupDoc.appendChild(rootNode);

    QTextStream backupStream(&backup);
    backupDoc.save(backupStream, 4);
    backup.close();

//--------------------------------结束新建--------------------------------

 

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