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

    XML,全称为 可扩展标记语言”(extensible markup language).是一种非常方便的数据交换数据存储的工具。

    我们在取得一个XML格式的文件后,需要作句法分析去提取发布方提供的信息。而QtXML提供了很好的支持,包括DOM方式的实现和SAX方式的实现。

    二者主要区别是:

    DOM (Document Object Model) 实现方式操作非常简单,但不适合处理过大文件;而SAX实现方式是能处理很大的XML文件,但是需要开发者写一些复杂的代码。Qt提供了对应于这两种用于 读取、操作和编写XML的实现类,分别是QDomDocument类和QXmlStreamReader,这里我们选QDomDocument来处理XML文件。

     

    首先是读取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();

     

         QFile file(fileName);
         
    if(!file.open(QFile::ReadOnly | QFile::Text)) {
             
    return false;
          }
         QString errorStr;
         
    int errorLine;
         
    int errorColumn;
     
          QDomDocument doc;
         
    if(!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))

    { // setContent 是将指定的内容指定给QDomDocument 解析,第一参数可以是QByteArray或者是文件名等。
             return false;
          }
     
          file.close();

     

    可以通过 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 “<<node.nodeType();

    }

    通过上面的方法你就能知道每个节点的 名字和类型 了,节点名字就是 标记 的名字。打印出来对照着 xml文件看就很容易明白了。QDomNode类是一个父类, QDomDocumentQDomNode的一个子类,鉴于大部分 QDomNode的类型 都是QDomDocument.那么你可以使用toDocument()函数将QDomNode类型转换成QDomDocument.

    QString text=node.toElement().text();

    上面的语句可以将一个节点里的 文本 取出也就是 标记内的文本 部分。使用该函数获得的编码就已经是unicode格式了,不需要再做转换。

     

     

    然后是写入XML

    创建节点,将其写入XML文件,主要操作包括:

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

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

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

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

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

    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>

        </note>

    </Notes>

         QFile  file(fileName);
         
     if(!file.open(QFile::ReadOnly | QFile::Text))
             
    returnfalse;
          QString errorStr;
         
     interrorLine;
         
     interrorColumn;
          QDomDocument doc;
         
     if(!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
             
    returnfalse;
     
          file.close();
     
          QDomElement root = doc.documentElement();
         
     if(root.tagName() != "root")
             
    returnfalse;
     
          QDomElement element =   doc.createElement_x_x_x(
    "file");


          QDomElement el_address = doc.createElement_x_x_x(
    "address");
          QDomText text_address = doc.createTextNode(
    "text");


          el_address.a
     (text_address);
          element.
    a
     (el_address);
     
          QDomElement el_path = doc.createElement_x_x_x_x("path");
          QDomText text_path = doc.createTextNode(
    "text");


          el_path.a (text_path);
          element.
    a
     (el_path);
     
          root.
    a
     (element);
     
          QFile f(fileName);
         if(!f.open(QFile::WriteOnly | QFile::Text))
             return false;
     
          QTextStream out(&f);

          out.setCodec("UTF-8"); //中文编码

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

          doc.save(out, 3);
          f.close();

    这里所有需要append的字节点只能由QDomDocument对象create,而不能直接new,否则永远也看不到新添加的节点,另外,读取和写入的文件流必须创建两个,否则会将需要写入的文档继续在原来文档的后面,造成多个根节点。

  • 相关阅读:
    程序员的7中武器
    需要强化的知识
    微软中国联合小i推出MSN群Beta 不需任何插件
    XML Notepad 2006 v2.0
    Sandcastle August 2006 Community Technology Preview
    [推荐] TechNet 广播 SQL Server 2000完结篇
    《太空帝国 4》(Space Empires IV)以及 xxMod 英文版 中文版 TDM Mod 英文版 中文版
    IronPython 1.0 RC2 更新 1.0.60816
    Microsoft .NET Framework 3.0 RC1
    《Oracle Developer Suite 10g》(Oracle Developer Suite 10g)V10.1.2.0.2
  • 原文地址:https://www.cnblogs.com/cy568searchx/p/3635698.html
Copyright © 2011-2022 走看看