I made an XML export of the records in my database and I want to make an import function.
My XML looks like this:
<Group1>
<Group1Element>
<ID>1</ID>
<Name>First</Name>
</Group1Element>
<Group1Element>
<ID>2</ID>
<Name>Second</Name>
</Group1Element>
</Group1>
<Group2>
<Group2Element>
<ID>1</ID>
<Nickname>fir</Nickname>
<Name>First Name</Name>
</Group2Element>
<Group2Element>
<ID>2</ID>
<Nickname>sec</Nickname>
<Name>Second Name</Name>
</Group2Element>
</Group2>
Group1 is a table from my database and Group2 is an another table. I have two methods to read the two table records from the XML file, but doesn't matter what I do only the first group in the XML is imported. If Group1 is the first then that is imported, if Group2 is the first in the file, then that's imported. When the second group's read method runs (in this case readGroup2()), the first while loop runs only three times and stops after "Group1 -> Group1Element -> ID". What am I missing here?
Here is my code:
void Import::readGroup1(QString filepath)
{
QFile file(filepath)
file.open(QIODevice::ReadOnly);
QXmlStreamReader stream(&file);
while(stream.readNextStartElement() && !stream.isEndDocument())
{
if(stream.name() == "Group1Element")
{
qDebug() << " ";
while(stream.readNext() && !stream.isEndElement())
{
if(stream.name() == "ID" || stream.name() == "Name")
{
qDebug() << stream.readElementText();
}
}
}
}
file.close();
}
void Import::readGroup2(QString filepath)
{
QFile file(filepath)
file.open(QIODevice::ReadOnly);
QXmlStreamReader stream(&file);
while(stream.readNextStartElement() && !stream.isEndDocument())
{
if(stream.name() == "Group2Element")
{
qDebug() << " ";
while(stream.readNext() && !stream.isEndElement())
{
if(stream.name() == "ID" ||
stream.name() == "Nickname" ||
stream.name() == "Name")
{
qDebug() << stream.readElementText();
}
}
}
}
file.close();
}