QT 遍历多层次的XML文档

          网上的例子大多是简单的例子的使用,但是对于多层次的或是未知层次的没有相应的记录。自己写了一个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 failed\n");
        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);
 
                }
            }
        }
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值