Qt5对XML文件操作

摘要:
doc.setContent){file.close();return;}文件close();QDomElementroot=文档。documentElement();//返回根节点qDebug()˂˂root。nodeName();QDomNodenode=根。firstChild();//在(!node.isNull())时获取第一个子节点//如果节点不为空{If//如果节点是元素{QDomElemente=node.toElement();//将其转换为元素。请注意,元素和节点是两个数据结构。事实上,qDebug()˂˂e.tagName()˂˂“”˂˂e.attribute˂˂“”˂/e.attributes;//打印键值对。tagName和nodeName是相同的,QDomNodeListlist=e.childNodes() ; 对于//遍历子元素,count和size可用于计算标记的数量{QDomNode=list.at;ifqDebug()˂˂n.nodeName()˂˂“:”˂˂n.toElement().text();}}node=节点。nextSibling();//下一个同级节点nextSiblingElement()是下一个下级元素,几乎}}添加xml内容//添加xml内容voidAddXml(){//打开文件QFilefile;//相对路径、绝对路径和资源路径都可以是if(!file.Open)return;//添加一个级别1的子节点和元素QDomDocumentdoc;if(!

转自https://blog.csdn.net/hpu11/article/details/80227093

写入xml

//写xml
void WriteXml()
{
    //打开或创建文件
    QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
    if(!file.open(QFile::WriteOnly|QFile::Truncate)) //可以用QIODevice,Truncate表示清空原来的内容
        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); //方式一:创建属性  其中键值对的值可以是各种类型
    QDomAttr time=doc.createAttribute("time"); //方式二:创建属性 值必须是字符串
    time.setValue("2013/6/13");
    book.setAttributeNode(time);
    QDomElement title=doc.createElement("title"); //创建子元素
    QDomText text; //设置括号标签中间的值
    text=doc.createTextNode("C++ primer");
    book.appendChild(title);
    title.appendChild(text);
    QDomElement author=doc.createElement("author"); //创建子元素
    text=doc.createTextNode("Stanley 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

//读xml
void ReadXml()
{
    //打开或创建文件
    QFile file("test.xml"); //相对路径、绝对路径、资源路径都行
    if(!file.open(QFile::ReadOnly))
        return;

    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();

    QDomElement root=doc.documentElement(); //返回根节点
    qDebug()<<root.nodeName();
    QDomNode node=root.firstChild(); //获得第一个子节点
    while(!node.isNull())  //如果节点不空
    {
        if(node.isElement()) //如果节点是元素
        {
            QDomElement e=node.toElement(); //转换为元素,注意元素和节点是两个数据结构,其实差不多
            qDebug()<<e.tagName()<<" "<<e.attribute("id")<<" "<<e.attribute("time"); //打印键值对,tagName和nodeName是一个东西
            QDomNodeList list=e.childNodes();
            for(int i=0;i<list.count();i++) //遍历子元素,count和size都可以用,可用于标签数计数
            {
                QDomNode n=list.at(i);
                if(node.isElement())
                    qDebug()<<n.nodeName()<<":"<<n.toElement().text();
            }
        }
        node=node.nextSibling(); //下一个兄弟节点,nextSiblingElement()是下一个兄弟元素,都差不多
    }

}

 增加xml内容

//增加xml内容
void AddXml()
{
    //打开文件
    QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
    if(!file.open(QFile::ReadOnly))
        return;

    //增加一个一级子节点以及元素
    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        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就是在后面追加内容,就无效了
        return;
    //输出到文件
    QTextStream out_stream(&file);
    doc.save(out_stream,4); //缩进4格
    file.close();
}

 删减xml内容

//删减xml内容
void RemoveXml()
{
    //打开文件
    QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
    if(!file.open(QFile::ReadOnly))
        return;

    //删除一个一级子节点及其元素,外层节点删除内层节点于此相同
    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        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")  //以属性名定位,类似于hash的方式,warning:这里仅仅删除一个节点,其实可以加个break
            root.removeChild(list.at(i));
    }

    if(!file.open(QFile::WriteOnly|QFile::Truncate))
        return;
    //输出到文件
    QTextStream out_stream(&file);
    doc.save(out_stream,4); //缩进4格
    file.close();
}

 更新xml内容

//更新xml内容
void UpdateXml()
{
    //打开文件
    QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
    if(!file.open(QFile::ReadOnly))
        return;

    //更新一个标签项,如果知道xml的结构,直接定位到那个标签上定点更新
    //或者用遍历的方法去匹配tagname或者attribut,value来更新
    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();

    QDomElement root=doc.documentElement();
    QDomNodeList list=root.elementsByTagName("book");
    QDomNode node=list.at(list.size()-1).firstChild(); //定位到第三个一级子节点的子元素
    QDomNode oldnode=node.firstChild(); //标签之间的内容作为节点的子节点出现,当前是Pride and Projudice
    node.firstChild().setNodeValue("Emma");
    QDomNode newnode=node.firstChild();
    node.replaceChild(newnode,oldnode);

    if(!file.open(QFile::WriteOnly|QFile::Truncate))
        return;
    //输出到文件
    QTextStream out_stream(&file);
    doc.save(out_stream,4); //缩进4格
    file.close();
}

免责声明:文章转载自《Qt5对XML文件操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇第23周二PHP各版本的区别下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

jenkins参数化配置,pom.xml配置

1、要实现Jenkins参数化构建,要先在代码里写好能接收该参数value的配置,在pom.xml文件里加配置,如下:   1)<properties></properties>里添加参数的key和value,通过本地执行时,可以直接从pom文件获取,或者从config.properitites获取   l  <propert...

Sandcastle的使用

Quote:Sandcastle, created by Microsoft, is a tool used for creating MSDN-style documentation from .NET assemblies and their associated XML comments files. The current version is t...

关于dom4j解析XML的问题分享

最近在在做个程序需要将C#小工具转成java,因为需要涉及到操作xml文件所以需要引用dom4j; 使用dom4j解析XML时,要快速获取某个节点的数据,使用XPath是个不错的方法,dom4j的快速手册里也建议使 用这种方式,标题都写的这么阔气:Powerful Navigation with XPath。 方法是使用Document的selectNod...

使用JAVA调用KRPANO加密XML

KRPano自带的命令行工具krpanotools可以加密XML,具体的参数说明如下语法:   krpanotools32.exe encrypt [OPTIONS] inputfiles inputfiles 参数可以是任意个数的文件 (支持*) 选项:-h5 … 使用HTML5兼容的加密方式-bin … 使用仅支持Flash的加密方式-p ...

Dataset利用xsd读取xml,数值类型处理及验证

应该会有很多场景需要从xml文件读取数据,填充一个dataset。机器上没装数据库,个人觉得最好简便方法就是定义一个xml文件,模拟数据。默认,xml在datatable中的值都是字符串类型(Excel中输入数字,就知道是数值型)。如果需要dataset在调用readxml方法的时候,把是什么类型(比如xml中本意是整型,时间类型)自动转换成什么类型方便,...

通过在xml布局文件中设置android:onClick=""来实现组件单击事件

在布局中出现android:onClick=""语句: <Button android:id="@+id/call_button" android:onClick="callphone" android:layout_width="wrap_content" android:l...