xml文件格式
<sura_list> <sura number="1"> <aya number="1"> <translation><![CDATA[读取字段]]></translation> <footnotes></footnotes> </aya> <aya number="2"> <translation><![CDATA[读取字段]]></translation> <footnotes></footnotes> </aya> <aya number="3"> <translation><![CDATA[读取字段]]></translation> <footnotes></footnotes> </aya> <aya number="4"> <translation><![CDATA[读取字段]]></translation> <footnotes></footnotes> </aya> <aya number="5"> <translation><![CDATA[读取字段]]></translation> <footnotes></footnotes> </aya> <aya number="6"> <translation><![CDATA[读取字段]]></translation> <footnotes></footnotes> </aya> <aya number="7"> <translation><![CDATA[读取字段读取字段读取字段]]></translation> <footnotes></footnotes> </aya> </sura>
读取代码
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();