[QT Operation XML] QT lee y escribe archivos XML, QT modifica archivos XML

[QT Operation XML] QT lee y escribe archivos XML

Introducción a XML

Concepto: Lenguaje de marcado extensible Lenguaje de marcado extensible (Extensible: las etiquetas son todas personalizadas)
Función: Almacenar datos (1. Archivo de configuración, 2. Transmisión en la red)
Sintaxis XML básica:
1. El sufijo del documento xml.
2. La primera línea de xml debe definirse como una declaración de documento
3. Hay una y solo una etiqueta raíz en el documento xml
4. El valor del atributo debe estar entre comillas (ya sea simple o doble)
5. La etiqueta debe estar cerrada correctamente
6. El nombre de la etiqueta xml se distingue
Formato de declaración del documento de mayúsculas : <? Xml version = '1.0' encoding = 'UTF-8'?>
Versión: número de versión,
codificación de atributo requerida : método de codificación, le dice al motor de análisis la codificación del juego de caracteres método utilizado por el documento actual, el valor predeterminado: ISO-8859-1
independiente: independiente (valor, sí: no dependa de otros archivos; no: dependa de otros archivos)

Operación QT XML, escribir, leer, modificar

El archivo profesional agrega el módulo xml: QT + = xml
incluye el archivo de encabezado: #include

//写入XML
void MainWindow::writeXML()
{
    
    
    QFile file("test.xml");//打开或新建xml文件
    if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate))//Truncate表示清空原来的内容
    {
    
    
        QMessageBox::warning(this,"错误","文件打开失败");
        return;
    }

    QDomDocument doc;
    //写入xml头部
    QDomProcessingInstruction instruction;//添加处理指令
    instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);
    //添加根节点
    QDomElement root = doc.createElement("library");
    doc.appendChild(root);
    //添加第一个子节点,及其子元素
    QDomElement book = doc.createElement("book");
    book.setAttribute("id",1);//方法1,创建属性,键值对可以是各种类型
    QDomAttr time = doc.createAttribute("time");//方法2,创建属性,值必须是字符串
    time.setValue("2020/6/3");
    book.setAttributeNode(time);

    QDomElement title = doc.createElement("title");//创建子元素
    QDomText text = doc.createTextNode("C++ primer");//设置括号标签中间的值
    book.appendChild(title);
    title.appendChild(text);
    QDomElement author = doc.createElement("author");//创建子元素
    text = doc.createTextNode("Stanley B.Lippman");
    author.appendChild(text);
    book.appendChild(author);
    root.appendChild(book);

    //添加第二个子节点,部分变量只需重新赋值
    book=doc.createElement("book");
    book.setAttribute("id",2);
    time = doc.createAttribute("time");
    time.setValue("2007/5/25");
    book.setAttributeNode(time);
    title = doc.createElement("title");
    text = doc.createTextNode("Thinking in Java");
    book.appendChild(title);
    title.appendChild(text);
    author = doc.createElement("author");
    text = doc.createTextNode("Bruce Eckel");
    author.appendChild(text);
    book.appendChild(author);
    root.appendChild(book);

    //输出文件
    QTextStream out_stream(&file);
    doc.save(out_stream,4);//缩进4格
    file.close();
}

//读XML文件
void MainWindow::readXML()
{
    
    
    QFile file("test.xml");
    if(!file.open(QIODevice::ReadOnly))
    {
    
    
        QMessageBox::warning(this,"错误","读XML,文件打开失败");
        return;
    }
    QDomDocument doc;
    if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容
    {
    
    
        file.close();
        return;
    }
    file.close();
    QDomElement root = doc.documentElement();//返回根节点
    qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<"root.nodeName "<<root.nodeName();//打印根节点
    QDomNode node = root.firstChild();//获得第一个子节点
    while(!node.isNull())//如果节点不为空
    {
    
    
        if(node.isElement())//如果节点是元素
        {
    
    
            QDomElement e= node.toElement();//节点转换为元素
            //打印键值对,tagName和nodeName相同
            qDebug() << e.tagName()<< " " <<e.attribute("id")<<" "<<e.attribute("time");
            QDomNodeList list = e.childNodes();//子节点列表
            for(int i=0;i<list.count();i++)//遍历子节点
            {
    
    
                QDomNode n = list.at(i);
                if(n.isElement())
                    qDebug() << n.nodeName()<<":"<<n.toElement().text();
            }
        }
        node=node.nextSibling();//下一个兄弟节点
    }
}

//增加XML内容
//先把文件读进来,再重写
void MainWindow::addXML()
{
    
    
    QFile file("test.xml");
    if(!file.open(QIODevice::ReadOnly))
    {
    
    
        QMessageBox::warning(this,"错误","增加XML,文件打开失败1");
        return;
    }
    QDomDocument doc;
    if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容
    {
    
    
        file.close();
        return;
    }
    file.close();

    QDomElement root=doc.documentElement();
    QDomElement book=doc.createElement("book");
    book.setAttribute("id",3);
    book.setAttribute("time","1813/1/27");
    QDomElement title=doc.createElement("title");
    QDomText text;
    text=doc.createTextNode("Pride and Prejudice");
    title.appendChild(text);
    book.appendChild(title);
    QDomElement author=doc.createElement("author");
    text=doc.createTextNode("Jane Austen");
    author.appendChild(text);
    book.appendChild(author);
    root.appendChild(book);

    if(!file.open(QFile::WriteOnly|QFile::Truncate))//重写文件,如果不用truncate就是在后面追加内容,就无效了
    {
    
    
        QMessageBox::warning(this,"错误","增加XML,文件打开失败2");
        return;
    }
    QTextStream out_stream(&file);
    doc.save(out_stream,4);
    file.close();
}

//删减XML内容
void MainWindow::removeXML()
{
    
    
    QFile file("test.xml");
    if(!file.open(QIODevice::ReadOnly))
    {
    
    
        QMessageBox::warning(this,"错误","增加XML,文件打开失败1");
        return;
    }
    QDomDocument doc;
    if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容
    {
    
    
        file.close();
        return;
    }
    file.close();
    QDomElement root=doc.documentElement();
    QDomNodeList list = doc.elementsByTagName("book");//指定名称的节点列表
    for(int i=0;i<list.count();i++)
    {
    
    
        QDomElement e = list.at(i).toElement();
        if(e.attribute("time")=="2007/5/25")
            root.removeChild(list.at(i));
    }
    if(!file.open(QFile::WriteOnly|QFile::Truncate))//重写文件,如果不用truncate就是在后面追加内容,就无效了
    {
    
    
        QMessageBox::warning(this,"错误","删减XML内容,文件打开失败");
        return;
    }
    QTextStream out_stream(&file);
    doc.save(out_stream,4);
    file.close();
}

//更新XML内容
//如果了解XML结构,可以直接定位到指定标签上更新
//或者用遍历的方法去匹配tagname或者attribut,value来更新
void MainWindow::updateXML()
{
    
    
    QFile file("test.xml");
    if(!file.open(QIODevice::ReadOnly))
    {
    
    
        QMessageBox::warning(this,"错误","更新XML,文件打开失败1");
        return;
    }
    QDomDocument doc;
    if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容
    {
    
    
        file.close();
        return;
    }
    file.close();

    QDomElement root = doc.documentElement();//获得根节点
    QDomNodeList list = root.elementsByTagName("book");//指定名称的节点列表
    QDomNode node = list.at(list.count()-1).firstChild();//定位到第三个一级子节点的子元素
    QDomNode oldNode = node.firstChild();//标签之间的内容作为节点的子节点出现,当前是Pride and Projudice
    oldNode.setNodeValue("dalao");//修改元素内容
//    node.firstChild().setNodeValue("diannao");
//    QDomNode newNode = node.firstChild();
//    node.replaceChild(newNode,oldNode);

    if(!file.open(QFile::WriteOnly|QFile::Truncate))//重写文件,如果不用truncate就是在后面追加内容,就无效了
    {
    
    
        QMessageBox::warning(this,"错误","更新XML内容,文件打开失败2");
        return;
    }
    QTextStream out_stream(&file);
    doc.save(out_stream,4);
    file.close();
}

Demostración del efecto XML

<?xml version='1.0' encoding='UTF-8'?>
<library>
    <book id="1" time="2020/6/3">
        <title>C++ primer</title>
        <author>Stanley B.Lippman</author>
    </book>
    <book id="2" time="2007/5/25">
        <title>Thinking in Java</title>
        <author>Bruce Eckel</author>
    </book>
    <book id="3" time="1813/1/27">
        <title>diannao</title>
        <author>Jane Austen</author>
    </book>
</library>

Supongo que te gusta

Origin blog.csdn.net/weixin_40355471/article/details/106524574
Recomendado
Clasificación