zoukankan      html  css  js  c++  java
  • 用 Qt 中的 QDomDocument类 处理 XML 文件(下)

     

    QDomDocument doc;

    1).创建根节点:QDomElement root = doc.documentElement("rootName " );

    2).创建元素节点:QDomElement element = doc.createElement_x("nodeName");

    3).添加元素节点到根节点:root. appendChild(element);

    4).创建元素文本:QDomText nodeText=doc.createTextNode("text");

    5).添加元素文本到元素节点:element. appendChild(nodeText);

    在本项目中,假设便签的属性有序号、名字、内容、字体、字号、颜色、粗细、斜体、下划线这几项,则在文件中添加一个便签节点的操作如下:

    QDomDocument doc;

    instruction = doc.createProcessingInstruction("xml","version="1.0" encoding="UTF-8"");

    doc.appendChild(instruction);

    QDomElement root = doc.createElement_x("Notes");

    doc.appendChild(root);

    QDomElement note = doc.createElement_x("note");

    root.appendChild(note);

    QDomElement no = doc.createElement_x("no");

    note.appendChild(no);

    ...

    ...

    QDomText no_text = doc.createTextNode("001");

    ...

    ...

    则得到一个便签节点,将其保存到test.xml文件中,代码如下:

    QFile file("test.xml");

    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate |QIODevice::Text))

    return ;

    QTextStream out(&file);

    out.setCodec("UTF-8");

    doc.save(out,4,QDomNode::EncodingFromTextStream);

    file.close();

    则test.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>

    <Notes>

    <note>

    <no>001</no>

    <name>2010-05-10(13:53:24)</name>

    <content>A meeting!</content>

    <font>Script MT Bold</font>

    <fontSize>16</fontSize>

    <color> #00ffff</color>

    <weight>0</weight>

    <fontItalic>true</fontItalic>

    <fontUnderline>true</fontUnderline>

    </note>

    </Notes>

    上面是创建一个便签节点,若要继续添加便签节点,则需要在已有节点后增加一个新节点,并重写入XML文件。

    2. 加载、查找便签时要读取XML文档中的节点信息,DOM实现方式是将整个文档当作一个对象来装入内存进行处理,然后开发者可以访问这个对象中的每一个节点,每一个节点对应XML文件里

    的一个标记。

    主要操作包括:

    1).读取根节点:QDomElement root = doc.documentElement();

    2).读取第一个子节点:QDomNode node = root.firstChild();

    3).读取下一个子节点:node = node.nextSibling();

    4).匹配结点标记:node.toElement().tagName() == "note"

    5).读取节点文本:no = childNode.toText().data();
    //========================遍历节点================
    可以通过doc.childNodes()获得doc的所有的子节点列表QDomNodeList。比如

    QDomNodeList list=doc.childNodes();

    for(int i=0;i<list.count();i++){

    QDomNode node=list.at(i);//好的风格当然是把定义写在外面:(

    //qDebug()<<”node name is “<<node.nodeName();

    //qDebug()<<”node type is “<<.nodeType();

    }
    //==============================================

    以下是项目中读取便签属性的函数实现代码:

    void MainWindow::parseAttr(const QDomElement &element)

    {

    QString no,name,content,font,fontSize,color;

    QDomNode node = element.firstChild();

    while (!node.isNull()) {

    if (node.toElement().tagName() == "note") {//匹配note节点

    parseAttr(node.toElement());

    } else if (node.toElement().tagName() == "no") {//匹配属性no

    QDomNode childNode = node.firstChild();

    if (childNode.nodeType() == QDomNode::TextNode) {

    no = childNode.toText().data();

    }

    }

    else if (node.toElement().tagName() == "name") //匹配属性name

    ...

    ...

    node = node.nextSibling();//读取兄弟节点

    }

    }

    3. 删除便签时,要删除相应的XML节点,用到的主要函数为:root.removeChild(node); 但在删除某个节点后要重写整个文件。

    以上对XML文件的重写操作是必须的,因此在文件的打开方式中要加上QIODevice::Truncate,表示覆盖重写。目前还没有找到可以直接修改文件的方法,但若文件很大的情况下,必须考虑相

    应的效率问题。

    由于本项目在启动时需要将所有便签加载到内存,因此选用DOM方式比较合适,但如果处理的XML文件很大,而且不需要全部读到内存时,可以用SAX实现方式,它按阶段将文档读取到内存中,

    在碰到标签或者其它阶段的时候,可以调用开发者预先设计好的回调函数去处理,这样效率比DOM方式更高。


    4.读取XML文件内容:
    //===================打开document===============add me ======
     QString xmlPath = "filename.xml";
     QFile file(xmlPath);
     if (!file.open(QFile::ReadOnly | QFile::Text))
      return ;

     QString errorStr;
     int errorLine;
     int errorColumn;

     QDomDocument doc;
     if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
      return ;
     file.close();
    //也可以用doc.setContent(&file)带一个参数就行。
    //=============================================================

    然后按照上面方式遍历数据,修改后在以上面相同的方式写入到XML文件中。


    //=========================================附加一段代码 创建XML文件===============
    int main( int argc, char **argv )
    {
     QApplication a( argc, argv );

     QString xmlPath = "InitInfo.xml";
     QFile file(xmlPath);
     if (!file.open(QFile::ReadOnly | QFile::Text))
      return 1;
     QString errorStr;
     int errorLine;
     int errorColumn;
     QDomDocument doc;
     if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
      return 1;
     file.close();

     QDomElement root = doc.documentElement();
     QString str = root.tagName();
     if (root.tagName() != "Root")
      return 1;

     QDomElement element =  doc.createElement_x("device3");
     element.setAttribute("ChildName","Content");
     QDomElement el_address = doc.createElement_x("Content ");
     el_address.setAttribute("key","equipmentname");
     el_address.setAttribute("value","CSC101B");
     el_address.setAttribute("name","装置名称");
     //QDomText text_address = doc.createTextNode("key");
     //el_address.appendChild(text_address);

     element.appendChild(el_address);
     QDomElement el_path = doc.createElement_x("path");
     QDomText text_path = doc.createTextNode("aa");
     el_path.appendChild(text_path);
     element.appendChild(el_path);
     root.appendChild(element);
     QFile f(xmlPath);
     if (!f.open(QFile::WriteOnly | QFile::Text))
      return 1;
     QTextStream out(&f);
     QTextCodec * codec = QTextCodec::codecForName("GBK");
     out.setCodec(codec);
     QDomDocument xmlDoc;
     QString strHead("version="1.0" encoding="GB2312"");
     xmlDoc.appendChild(xmlDoc.createProcessingInstruction("xml", strHead));
     out<<xmlDoc.toString();
     root.save(out, 3);
     f.close();

     return 0;
    }

  • 相关阅读:
    [运维-安全]CentOS7.0环境下安装kangle和easypanel
    (转)FastDFS文件存储
    使用mybatis-generator-core自动生成代码
    SSM框架中常用的配置文件
    Spring MVC文件上传和下载
    Spring MVC-拦截器
    Spring MVC之JSON数据交互和RESTful的支持
    Spring MVC数据绑定(二)
    Spring MVC数据绑定(一)
    Spring MVC简介
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4102509.html
Copyright © 2011-2022 走看看