Qt采用Dom方式读写XML

XML文件如下所示:

<?xml version="1.0" encoding="GBK"?>
<Catalog name = "树形目录">
        <View id = "default">
                <任务年度/>
                <任务编号/>
                <任务名称/>
	</View>
        <View id = "1">
                <任务名称/>
                <任务年度/>
                <任务编号/>
	</View>
        <View id = "2">
                <任务年度/>
                <任务名称/>
                <任务编号/>
        </View>
</Catalog>

  读文件:

if("" == fileName)
    {
        qDebug()<<"Filename is Null";
        return;
    }
    QFile file(DirectorOf("xml").absoluteFilePath(fileName));
    if(!file.open(QFile::ReadOnly | QFile::Text))
        qDebug()<<"open file"<<fileName<<"failed, error:"<<file.errorString();
    /*解析Dom节点*/
       QDomDocument    document;
       QString         strError;
       int             errLin = 0, errCol = 0;
       if( !document.setContent(&file, false, &strError, &errLin, &errCol) ) {
           qDebug()<<"parse file failed at line"<<errLin<<",column"<<errCol<<","<<strError;
           return;
       }

       if( document.isNull() ) {
           qDebug()<<"document is null !";
           return;
       }

       QDomElement root = document.documentElement();
       qDebug()<<root.tagName();

       QDomElement catalogs = root.firstChildElement();
       if( catalogs.isNull() )
           return;
       else
           qDebug()<<catalogs.tagName();
       while(!catalogs.isNull())
       {
           QString tag = catalogs.attributeNode("id").value();
           QStringList child;
           QPair<QString,QStringList> pair;
           for(int i = 0;i < catalogs.childNodes().size();i++)
               child<<catalogs.childNodes().at(i).nodeName();
           pair.first = tag;
           pair.second = child;
           catalogList.append(pair);
           catalogs = catalogs.nextSiblingElement();
       }
       file.close();

  写入XML

QFile file(DirectorOf("xml").absoluteFilePath(xmlName));
       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))
            return false;
       file.close();
       QDomElement root = doc.documentElement();
       if(root.tagName() != "Catalog")
           return false;
       QDomElement element =  doc.createElement("View");
       QDomAttr idAttr = doc.createAttribute("id");
       element.setAttributeNode(idAttr);
       element.setAttribute("id",typeName);
       for(int i = 0;i < catalogs.size();i++)
       {
           QDomElement cataItem = doc.createElement(catalogs.at(i));
           element.appendChild(cataItem);
       }
       root.appendChild(element);
      /* QDomProcessingInstruction instruction;
       instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"GBK\"");
       doc.appendChild(instruction);*/
       QFile f(DirectorOf("xml").absoluteFilePath(xmlName));
           if(!f.open(QFile::WriteOnly | QFile::Text))
               return false;
       QTextStream out(&f);
       doc.save(out,4);
       f.close();
       return true;

posted on 2012-06-18 12:14  sprzhing  阅读(10639)  评论(0编辑  收藏  举报

导航