Qt读取XML文件

xml文件格式



	
		
		
	
	
	
		
		
	
	
	
		
		
	
	
	
		
		
	
	
	
		
		
	
	
	
		
		
	
	
	
		
		
	

读取代码

    QFile file;         
    QString nodename; 
    QString outData;

    file.setFileName(QDir::currentPath() + "\\doc\\chinese_makin_v1.0.2-xml.1.xml");
    if (!file.exists())
    {
        qDebug() << "xml path error";
    }
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))   
    {
        qDebug() << "mxl open error";
    }
    else
    {
        QXmlStreamReader xmlreader(&file);   
        while (!xmlreader.atEnd() || !xmlreader.hasError())
        {
            xmlreader.readNextStartElement();          
            nodename = xmlreader.name().toString();    
            if (nodename == "sura" && xmlreader.isStartElement())
            {
                QString str = xmlreader.attributes().value("number").toString();
                outData.append(QString("\n%1 \n").arg(str));
                while (!(nodename == "sura" && xmlreader.isEndElement()))
                {
                    xmlreader.readNextStartElement();
                    nodename = xmlreader.name().toString();
                    if (nodename == "aya" && xmlreader.isStartElement())
                    {
                        QString str = xmlreader.attributes().value("number").toString();
                        while (!(nodename == "aya" && xmlreader.isEndElement()))
                        {
                            xmlreader.readNextStartElement();
                            nodename = xmlreader.name().toString();
                            if (nodename == "translation" && xmlreader.isStartElement())
                            {
                                while (!(nodename == "translation" && xmlreader.isEndElement()))
                                {
                                    QString str = xmlreader.readElementText();
                                    outData.append(QString("%1 \n").arg(str));
                                }
                            }
                            //else if (nodename == "footnotes" && xmlreader.isStartElement())
                            //{
                            //    while (!(nodename == "footnotes" && xmlreader.isEndElement()))
                            //    {
                            //        QString str = xmlreader.readElementText();
                            //        qDebug() << "footnotes" << str;
                            //    }
                            //}
                        }
                    }
                }
            }
        }
    }

    file.close();

你可能感兴趣的:(qt,xml)