加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

QT 遍历多层次的XML文档

发布时间:2020-12-16 09:16:30 所属栏目:百科 来源:网络整理
导读:网上的例子大多是简单的例子的使用,但是对于多层次的或是未知层次的没有相应的记录。自己写了一个QT版的遍历XML的类NodeIterator nodeiterator.h文件 #ifndef NODEITERATOR_H #define NODEITERATOR_H #include QDomDocument class NodeIterator { public :

网上的例子大多是简单的例子的使用,但是对于多层次的或是未知层次的没有相应的记录。自己写了一个QT版的遍历XML的类NodeIterator

nodeiterator.h文件

#ifndef NODEITERATOR_H
#define NODEITERATOR_H
#include <QDomDocument>
 
class NodeIterator
{
public:
    NodeIterator();
    NodeIterator(QDomNode root);
    bool hasNext();
    QDomNode next();
private:
    QDomNode root, current;
 
    bool isHasNext;
};
 
#endif // NODEITERATOR_H
nodeiterator.cpp文件
#include "nodeiterator.h"
 
NodeIterator::NodeIterator()
{
 
 
}
NodeIterator::NodeIterator(QDomNode root)
{
    this->root = root;
    current = root;
}
 
bool NodeIterator::hasNext(){
 
    if (!root.isNull()) {
 
        //是否当前节点有子节点
        if (!current.isNull() && current.hasChildNodes()) {
 
            //将第一个子节点变成当前节点
            current = current.firstChildElement();
            isHasNext = true;
 
        } else if (!current.isNull()&& !current.nextSiblingElement().isNull()) {
 
 
            current = current.nextSiblingElement();
            isHasNext = true;
 
        } else if (!current.isNull()) {
 
            while (!current.isNull() && current != root && current.nextSiblingElement().isNull()) {
 
                current = current.parentNode();
            }
 
            if (!current.isNull() && current != root) {
 
                current = current.nextSiblingElement();
                isHasNext = true;
 
            } else {
 
                isHasNext = false;
            }
 
        } else {
 
            isHasNext = false;
        }
 
    } else {
 
        isHasNext = false;
    }
 
    return isHasNext;
}
QDomNode NodeIterator::next() {
    return current;
}
 
 有了这个类就可以根据名称检索相应的节点 
 
 
 QFile *file=new QFile("D:/qt project/SymbolEdit/symbol.xml");
 
    if( !file->open(QFile::ReadOnly)){
        qDebug()<<"open file failed ";
        return;
    }
 
    QDomDocument   *document=new QDomDocument;
    QString         strError;
    int        errLin = 0, errCol = 0;
 
    if( !document->setContent(file, false, &strError, &errLin, &errCol) ) {
        printf( "parse file failedn");
        return;
    }
 
    if( document->isNull() ) {
        printf( "document is null !n" );
        return;
    }
 
    QDomElement root = document->documentElement();
    //QIterator<Node>
 
    if (!root.isNull()) {
 
        QDomNode current;
 
        QString name , epTypeAlisaName ;
 
        for (NodeIterator *it = new NodeIterator((QDomNode)root); it->hasNext();) {
 
            current = it->next();
 
            if (!current.isNull() && current.isElement()) {
 
                name = current.nodeName();
                //如果是电力图元的节点
                if (!name.isNull() && name=="epType") {
                    epTypeAlisaName = current.toElement().attribute("alisaname");
                    QString electricType = current.toElement().attribute("electricType");
                    epTypeAlisaNames->append(epTypeAlisaName);
                    epTypeAlisaNameToElectricTypeName->insert(epTypeAlisaName,electricType);
 
                }
            }
        }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读