0

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();
}
1
  • Have you actually stepped through this code in a debugger to see what's happening? In its current form this isn't really a meaningful question. Commented Oct 9, 2016 at 19:51

1 Answer 1

1

I believe it only reads the first block because the reader will read until the end of the first element is reached. I would suggest to change your XML to the following format:

<Groups>
  <Group1>
  </Group1>
  <Group2>
  </Group2>
</Groups>

Then, implement logic to handle your different groups in one function.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it solves the problem partially. I changed the XML structure based on your answer (I removed the <Group1> and <Group2> tags), so it's just the group elements (<Group1Element> and <Group2Element>) within a big group (<Groups>). This way it works. But I want to separate the group elements by groups too. But if I put the group elements into Group1 and Group2, it fails again, even if they're in one big group (<Groups>).
Okay, found the problem, it works as you said, but I have to change readNextStartElement() to simple readNext(). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.