Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved Best Way to Read XML Tags w/o Specifying Tag's Name

    General and Desktop
    xml parsing xml xmllist xmllistmodel
    3
    3
    1009
    Loading More Posts
    • 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.
    • M
      masshakar last edited by masshakar

      I just started Qt development yesterday. I'm trying to find a way to read an XML file, without specifying the tags' names (we have a variety of tags, and we cannot enumerate them all; some may/may not be children of a specific tag). I looked into qxmlstreamreader, but that still requires you to specify the tags' names.

      How should I proceed? And thanks in advance.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        What exactly do you want to do with the content of that file ?

        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 Reply Quote 0
        • P
          panosk last edited by

          Hi,
          Creating a very "abstract" xml parser is not easy and it's quite error-prone. You have to know some (or at least one) parent or common tag names in order to have a starting point for any deeper "abstract" parsing. Basically, you have to move the parser step by step checking everything in the way. For example, you could use a simple function as an entry point for the following function when you meet a specific tag:

          void XmlReader::readFragment()
          {
              Q_ASSERT(m_reader->isStartElement()); //Make sure the xmlreader is at an opening tag
              const QString p = m_reader->qualifiedName().toString(); // From now on, you will work with this opening tag
              
          	//Do what you want, for example handle attributes and their values
          	for (auto i = 0; i < m_reader->attributes().count(); ++i) {
                  ...
              }
              m_reader->readNext(); //Move on
          	...
          	
          	//Do everything manually like this
          	if (m_reader->isCharacters) {
          		...
          	} else if (m_reader->isEndElement()) {
          		...
          	}
          	
          	//or like this
                  //Eventually, you have to check for the closing current tag, exit the function,
                  //and return the control to the "entry point" function
          	while (!m_reader->isEndElement() && !m_reader->qualifiedName().toString() == p) { 
          		... //Do your work
                          m_reader->readNext();
          	}
          	
          }
          

          However, with this approach, things can get extremely complicated if there are nested tags, for example:

          <p>
             <note>
                  <p>...</p>
             </note>
          </p>
          

          Maybe it would be easier to subclass QXmlStreamReader but it would help to provide some sample xml data and more info on what exactly you want to do.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post