<?xml version="1.0"?><XMLBIBLE biblename="Russian"> <BIBLEBOOK bname="Genesis"> <CHAPTER cnumber="1"> <VERS vnumber="1">In the beginning God created the heavens and the earth.</VERS> </CHAPTER> </BIBLEBOOK></XMLBIBLE>
QMap<QString, QString> Bible::parseBook(QXmlStreamReader& xml) { QMap<QString, QString> xmlbible; QXmlStreamAttributes attributes = xml.attributes(); if(xml.tokenType() != QXmlStreamReader::StartElement && xml.name() == "XMLBIBLE") { return xmlbible; } if(attributes.hasAttribute("biblename")) { xmlbible["biblename"] = attributes.value("biblename").toString(); } if(xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "BIBLEBOOK") { if(attributes.hasAttribute("bname")) { xmlbible["bname"] = attributes.value("bname").toString(); } } xml.readNext(); while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "XMLBIBLE")) { if(xml.tokenType() == QXmlStreamReader::StartElement) { /*название книги*/ if(xml.name() == "BIBLEBOOK") { this->addElementDataToMap(xml, xmlbible); } /*номер главы*/ if(xml.name() == "CHAPTER") { this->addElementDataToMap(xml, xmlbible); } /*номер текста*/ if(xml.name() == "VERS") { this->addElementDataToMap(xml, xmlbible); } } xml.readNext(); } return xmlbible;}
XML<XMLBIBLE biblename="Russian">
XML</XMLBIBLE>
C++ (Qt) while(!(token == QXmlStreamReader::EndElement && xml.name() == "XMLBIBLE")) { if(token == QXmlStreamReader::StartElement && xml.name() == "XMLBIBLE") { books.append(this->parseBook(xml)); } xml.readNext(); }
C++ (Qt)void Bible::parseXML(){ QFile* file = new QFile("C:\\Users\\Adm\\Documents\\Visual Studio 2008\\Projects\\Project1\\Bible\\Win32\\Debug\\russian.xml"); if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::critical(this, "Bible::parseXML", "Couldn't open russian.xml", QMessageBox::Ok); return; } QXmlStreamReader xml(file); QList< QMap<QString,QString> > books; while(!xml.atEnd() && !xml.hasError()) { QXmlStreamReader::TokenType token = xml.readNext(); if(token == QXmlStreamReader::StartDocument) { continue; } if(token == QXmlStreamReader::StartElement) { if(xml.name() == "XMLBIBLE") { books.append(this->parseBook(xml)); } if(xml.name() == "BIBLEBOOK") { books.append(this->parseBook(xml)); } if(xml.name() == "CHAPTER") { books.append(this->parseBook(xml)); } if(xml.name() == "VERS") { books.append(this->parseBook(xml)); } } } if(xml.hasError()) { QMessageBox::critical(this, "Bible::parseXML", xml.errorString(), QMessageBox::Ok); } xml.clear(); this->addBooksToUI(books);}
C++ (Qt)while(!xml.atEnd()) { QXmlStreamReader::TokenType token = xml.readNext(); if(token == QXmlStreamReader::StartDocument) { continue; } if(token == QXmlStreamReader::StartElement) { QXmlStreamAttributes attributes = xml.attributes(); if(xml.name() == "XMLBIBLE") { if(attributes.hasAttribute("biblename")) { xmlbible["biblename"] = attributes.value("biblename").toString(); } } } if(token == QXmlStreamReader::StartElement) { QXmlStreamAttributes attributes = xml.attributes(); if(xml.name() == "BIBLEBOOK") { if (attributes.hasAttribute("bname")) { xmlbible["bname"] = attributes.value("bname").toString(); } } } if(token == QXmlStreamReader::StartElement) { QXmlStreamAttributes attributes = xml.attributes(); if(xml.name() == "CHAPTER") { if(attributes.hasAttribute("cnumber")) { xmlbible["cnumber"] = attributes.value("cnumber").toString(); } } } if(token == QXmlStreamReader::StartElement) { QXmlStreamAttributes attributes = xml.attributes(); if(xml.name() == "VERS") { if(attributes.hasAttribute("vnumber")) { xmlbible["vnumber"] = attributes.value("vnumber").toString(); } } } xml.readNext(); }