finiteloop
finiteloop

Reputation: 4496

QDomDocument: error occurred while parsing element

I am using a QDomDocument to parse through an XML file in Qt. The code to do so is as follows:

QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("(*.xml)"));

QDomDocument domDocument;

try {
    QString qs;
    int y,j;
    if (!domDocument.setContent(fileName, false, &qs, &y, &j))
    {
        cout << "error: " << qs.toStdString() << " " << y << " " << j << endl;
        return;
    }
} catch (...)
  {
      //handle error
  }

The document I am opening with the getOpenFileName contains the following:

<list>
    <list_name>List1</list_name>
    <task>
        <task_name>Task1a</task_name>
        <task_due_date></task_due_date>
        <task_note></task_note>
    </task>
    <task>
        <task_name>Task1b</task_name>
        <task_due_date></task_due_date>
        <task_note></task_note>
    </task>
</list>

the call to setContent() returns false, causing the following message to be printed:

error: error occurred while parsing element 1 1

I can't figure out what is wrong that is causing this error to occur. Any help would be appreciated.

Upvotes: 2

Views: 3172

Answers (1)

sgibb
sgibb

Reputation: 25726

There is no QDomDocument::setContent method which accepts a filename. IMHO you misunderstood the following method: http://qt-project.org/doc/qt-4.8/qdomdocument.html#setContent-5 . Your code tries to parse the filename string.

Use a QFile object instead:

QFile file(fileName);

if (file.open(QIODevice::ReadOnly)) {
    QDomDocument domDocument;

    QString errorStr;
    int errorLine;
    int errorColumn;

    if (!domDocument.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
        qDebug() << errorStr << errorLine << errorColumn;
}

Upvotes: 2

Related Questions