如何读取具有相同标记名称的XML

时间:2018-01-05 10:03:41

标签: qt qt4 qt5 qxmlstreamreader

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CINEMAS_PROJECT_FILE>
<CinemasProject XMLversion="1">
    <Cascade>
        <ChildrenIDs>
            <JobName>1</JobName>
            <JobName>2</JobName>
            <JobName>3</JobName>
        </ChildrenIDs>
    </Cascade>

如何读取XML中具有相同标记的元素&#34; JobName&#34;使用QXmlStreamReader

1 个答案:

答案 0 :(得分:1)

QFile* file = new QFile(paths);

if(!file->open(QIODevice::ReadOnly | QIODevice::Text)){
    qDebug() << "Failed To Open Xml";
    return;
}

QXmlStreamReader* xml = new QXmlStreamReader(file);

while(!xml->atEnd()){

    QXmlStreamReader::TokenType token = xml->readNext();

    if(token == QXmlStreamReader::StartDocument){
        continue;
    }

    if(xml->name() == "JobName"){
          qDebug() << xml->readElementText();
    }
}