Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make xml parser more clear
Qt 6.11 is out! See what's new in the release blog

How to make xml parser more clear

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Imhotep
    wrote on last edited by
    #1

    I have an xml file built in this way:

    @<?xml version="1.0" encoding="UTF-8"?>
    <Draw>
    <Input>
    <Cells>
    <width>100</width>
    <height>28</height>
    </Cells>
    <Column>custom</Column>
    <Custom>
    <header id="0">one</header>
    <header id="1">two</header>
    <header id="2">three</header>
    <header id="3">four</header>
    <header id="4">five</header>
    </Custom>
    </Input>
    <Output>
    <Cells>
    <width>82</width>
    <height>20</height>
    </Cells>
    <Column>upper</Column>
    <Custom>
    <header id="0">alfa</header>
    <header id="1">beta</header>
    <header id="2">gamma</header>
    <header id="3">delta</header>
    <header id="4">epsilon</header>
    </Custom>
    </Output>
    </Draw>
    @

    And I'm trying to extrapolate the values ​​of the header tag: since we have two sets of header tags (Input and Output) the only working code that I managed to work for now is this:

    @
    void MainWindow::readXmlFile() {
    QString target;
    QFile* file = new QFile("System/Settings.xml");
    /* If we can't open it, let's show an error message. /
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) return;
    QXmlStreamReader xmlReader(file);
    /
    We'll parse the XML until we reach end of it./
    while(!xmlReader.atEnd() && !xmlReader.hasError()) {
    QXmlStreamReader::TokenType token = xmlReader.readNext();
    if(token == QXmlStreamReader::StartDocument) {
    continue;
    }
    /
    If token is StartElement, we'll see if we can read it./
    if (token == 4) {
    if (xmlReader.name() == "Input" || xmlReader.name() == "Output") {
    target = xmlReader.name();
    if ((!(xmlReader.tokenType() == QXmlStreamReader::EndElement && xmlReader.name() == target))) {
    if (xmlReader.tokenType() == QXmlStreamReader::StartElement) {
    if (xmlReader.name() == "header") {
    //headers->append(xmlReader.readElementText());
    qDebug() << xmlReader.readElementText();
    }
    }
    }
    }
    }
    }
    /
    Error handling. */
    if(xmlReader.hasError()) {
    QMessageBox::critical(this,
    "QXSRExample::parseXML",
    xmlReader.errorString(),
    QMessageBox::Ok);
    }
    xmlReader.clear();
    }
    @

    Since this code seems very repetitive, especially from line 15 to 18, could you help me to make it a little cleaner?

    Thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should have a look at the QXmlStream Bookmarks Example

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved