Why can't I parse a XML file using QXmlStreamReader from Qt?

后端 未结 5 1771
长发绾君心
长发绾君心 2021-02-14 06:43

I\'m trying to figure out how QXmlStreamReader works for a C++ application I\'m writing. The XML file I want to parse is a large dictionary with a convoluted structure and plent

相关标签:
5条回答
  • 2021-02-14 06:45

    The file is not UTF-8 encoded. Change the encoding to iso-8859-1 and it will parse without error.

    <?xml version="1.0" encoding="iso-8859-1" ?>
    
    0 讨论(0)
  • 2021-02-14 06:49

    Try this Example i just copied it from my project it work for me.

    void MainWindow::readXML(const QString &fileName)
    {
    
    
    fileName = "D:/read.xml";
    
    QFile* file = new QFile(fileName);
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
    {
         QMessageBox::critical(this, "QXSRExample::ReadXMLFile", "Couldn't open xml file", QMessageBox::Ok);
         return;
    }
    
    /* QXmlStreamReader takes any QIODevice. */
    QXmlStreamReader xml(file);
    /* We'll parse the XML until we reach end of it.*/
    while(!xml.atEnd() && !xml.hasError())
    {
        /* Read next element.*/
        QXmlStreamReader::TokenType token = xml.readNext();
        /* If token is just StartDocument, we'll go to next.*/
        if(token == QXmlStreamReader::StartDocument)
            continue;
    
        /* If token is StartElement, we'll see if we can read it.*/
        if(token == QXmlStreamReader::StartElement) {
            if(xml.name() == "email") {
                ui->listWidget->addItem("Element: "+xml.name().toString());
                continue;
            }
        }
    }
    /* Error handling. */
    if(xml.hasError())
        QMessageBox::critical(this, "QXSRExample::parseXML", xml.errorString(), QMessageBox::Ok);
    
    //resets its internal state to the initial state.
    xml.clear();
    }
    
    void MainWindow::writeXML(const QString &fileName)
    {
    fileName = "D:/write.xml";
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
         QMessageBox::critical(this, "QXSRExample::WriteXMLFile", "Couldn't open anna.xml", QMessageBox::Ok);
         return;
    }
    QXmlStreamWriter xmlWriter(&file);
    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    //add Elements
    xmlWriter.writeStartElement("bookindex");
    ui->listWidget->addItem("bookindex");
    xmlWriter.writeStartElement("Suleman");
    ui->listWidget->addItem("Suleman");
    
    //write all elements in xml filexl
    xmlWriter.writeEndDocument();
    file.close();
    if (file.error())
        QMessageBox::critical(this, "QXSRExample::parseXML", file.errorString(), QMessageBox::Ok);
    
    
    }
    
    0 讨论(0)
  • 2021-02-14 06:53

    I'm answering this myself as this problem was related to three issues, two of which were brought up by the responses.

    1. The file actually wasn't UTF-8 encoded. I changed the encoding to iso-8859-1 and the encoding warning disappeared.
    2. The text() function doesn't work as I expected. I have to use readElementText() to read the entries' contents.
    3. When I try to readElementText() on an element that doesn't contain text, like the top-level <persons> in my case, the parser returns an "Expected character data" error and the parsing is interrupted. I find this behaviour strange (in my opinion returning an empty string and continuing would be better) but I guess as long as the specification is known, I can work around it and avoid calling this function on every entry.

    The relevant code section that works as expected now looks like this:

    while (!xml.atEnd() && !xml.hasError()) 
    {
        xml.readNext();
        if (xml.isStartElement())
        {
            QString name = xml.name().toString();
            if (name == "firstname" || name == "surname" || 
                name == "email" || name == "website")
            {
                cout << "element name: '" << name  << "'" 
                             << ", text: '" << xml.readElementText() 
                             << "'" << endl;
            }
        }
    }
    if (xml.hasError())
    {
        cout << "XML error: " << xml.errorString() << endl;
    }
    else if (xml.atEnd())
    {
        cout << "Reached end, done" << endl;
    }
    
    0 讨论(0)
  • 2021-02-14 06:56

    About the encoding: As baysmith and and hmuelner said, your file is probably incorrectly encoded (unless the encoding got lost when pasting it here). Try to fix that with some advanced text editor.

    The problem with your usage of text() is that it doesn't work as you expect it to. text() returns the content of the current token if it is of type Characters, Comment, DTD or EntityReference. Your current token is a StartElement, so it's empty. If you want to consume/read the text of the current startElement, use readElementText() instead.

    0 讨论(0)
  • 2021-02-14 06:56

    Are you sure your document is UTF-8 encoded? What editor did you use? Check how the ä-characters look like if you view the file without decoding.

    0 讨论(0)
提交回复
热议问题