QT 遍历多层次的XML文档

前端之家收集整理的这篇文章主要介绍了QT 遍历多层次的XML文档前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

nodeiterator.h文件

#ifndef NODEITERATOR_H@H_502_9@ 
#define NODEITERATOR_H@H_502_9@ 
#include <QDomDocument>@H_502_9@ 
@H_502_9@ 
class NodeIterator@H_502_9@ 
{@H_502_9@ 
public:@H_502_9@ 
    NodeIterator();@H_502_9@ 
    NodeIterator(QDomNode root);@H_502_9@ 
    bool hasNext();@H_502_9@ 
    QDomNode next();@H_502_9@ 
private:@H_502_9@ 
    QDomNode root, current;@H_502_9@ 
@H_502_9@ 
    bool isHasNext;@H_502_9@ 
};@H_502_9@ 
@H_502_9@ 
#endif // NODEITERATOR_H@H_502_9@ nodeiterator.cpp文件 

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

猜你在找的XML相关文章