旗下导航:搜·么
当前位置:网站首页 > XML教程 > 正文

Qt剖析XML的示例代码分享【XML教程】,Qt,XML

作者:搜教程发布时间:2019-12-01分类:XML教程浏览:40评论:0


导读:怎样运用QXmlStreamReader来剖析花样优越的XML,Qt的文档中指出,它是一种更快、更轻易的Qt本身的SAX剖析器(QXmlSimpleReader)的替换,它也较快,...
怎样运用QXmlStreamReader来剖析花样优越的XML,Qt的文档中指出,它是一种更快、更轻易的Qt本身的SAX剖析器(QXmlSimpleReader)的替换,它也较快,在某种情况下,比DOM(QDomDocument)更轻易。

XML文件:

剖析要领:

void ParseXML::parseXML(QString file_name)
{
if(file_name.isEmpty())  
        return;  
  
    QFile *file = new QFile(file_name);  
    if(!file->open(QFile::ReadOnly | QFile::Text))
{  
QMessageBox::information(NULL, QString("title"), QString("open error!"));
        return;  
    }  
    //QXmlStreamReader操纵任何QIODevice.
    QXmlStreamReader xml(file);
    QList> persons;
    //剖析XML,直到完毕
    while(!xml.atEnd() && !xml.hasError())
{
        //读取下一个element.
        QXmlStreamReader::TokenType token = xml.readNext();
        //假如猎取的仅为StartDocument,则举行下一个
        if(token == QXmlStreamReader::StartDocument)
{
            continue;
        }
        //假如猎取了StartElement,则尝试读取
        if(token == QXmlStreamReader::StartElement)
{
            //假如为persons,直接进入下一个
            if(xml.name() == "persons")
{
                continue;
            }
            
//假如为person,则对其举行剖析
            if(xml.name() == "person")
{
                persons.append(this->parsePerson(xml));
            }
        }
    }
  
    if(xml.hasError())
{
QMessageBox::information(NULL, QString("parseXML"), xml.errorString());
    }
    
//从reader中删除一切装备、数据,并将其重置为初始状况
    xml.clear();
}
 
QMap ParseXML::parsePerson(QXmlStreamReader& xml)
{
    QMap person;
    //搜检是不是猎取person
    if(xml.tokenType() != QXmlStreamReader::StartElement &&
            xml.name() == "person")
{
        return person;
    }
    //猎取person属性
    QXmlStreamAttributes attributes = xml.attributes();
    if(attributes.hasAttribute("id"))
{
        person["id"] = attributes.value("id").toString();
    }
    
//操纵下一个
    xml.readNext();
    while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
            xml.name() == "person"))
{
        if(xml.tokenType() == QXmlStreamReader::StartElement)
{
            if(xml.name() == "name")
{
                this->addElementDataToMap(xml, person);
            }
           
            if(xml.name() == "age")
{
                this->addElementDataToMap(xml, person);
            }
          
            if(xml.name() == "email")
{
                this->addElementDataToMap(xml, person);
            }
          
            if(xml.name() == "website")
{
                this->addElementDataToMap(xml, person);
            }
        }
     
        xml.readNext();
    }
QString id = person["id"];
    QString name = person["name"];
    QString age =  person["age"];
    QString email = person["email"];
    QString website = person["website"];
    return person;
}
 
void ParseXML::addElementDataToMap(QXmlStreamReader& xml,
                                      QMap& map) const
{
    if(xml.tokenType() != QXmlStreamReader::StartElement)
{
        return;
    }
    QString elementName = xml.name().toString();
    xml.readNext();
    
    if(xml.tokenType() != QXmlStreamReader::Characters) 
{
        return;
    }
    
    map.insert(elementName, xml.text().toString());
}

以上就是 Qt剖析XML的示例代码分享的细致内容,更多请关注ki4网别的相干文章!

标签:QtXML


欢迎 发表评论: