Nominate our 2022 Qt Champions!

[NEWBIE] simple XML reader - displaying text between tags.

  • Hello all,

    I'm a newbie here, as well as in the QT environment. I do have some very basic understanding of C++ (procedural, not object-oriented, unfortunately).
    What I'm currently struggling with is a simple XML parser. Basing on the literature and online tutorials I managed to cobble together "something" that works, partially. The problem is, it does not work precisely as intended :-) Here's the code snippet:

    and here's the program's output:

    What it does is reading the root and child nodes, but without parsing any text between them (Tove, Jani etc.). I would like to display both the node names and the text between them. I suspect I should put a "QXmlStreamReader" below the "else" if I want to display the text for child nodes, but to be honest - I suck at figuring out what exactly should be put in there.

    Thanks in advance for your help.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you saw the XML Bookmarks example ? It looks like it does what you need

  • Thanks for the tip, SGaist!

    I read up on it yesterday and compared the info with what I was able to write before. So what I did was reading another tutorial and adding the:

                if(name == "to" || name =="from"){
                string = xmlReader.readElementText();
                sl << string;"

    before the else sl<<

    It works, i.e. shows the text content between "from" and"to" XML tags. But that procedure above can get very tedious if you have ~40 different XML tags.

    What I was wondering was, is there any 'automagic' function that could read the text between the XML tags?
    That pastebin snippet is able to find XML tags and build a tree basing on tags found inside the file (not really looking for a specific text like "to" or "from") - so would it be possible to use another function that would just search for anything that is between the tags - and display that in the tree as well?

    Sorry if I express myself incorrectly, as I said - I still have a lot to learn.

    Thanks in advance for your help.

  • Hi Godfreyho,

    I don´t know if this is to take a sledgehammer to crack a nut for your
    intend, but if you want XML files to be parsed and output you can
    use a XSLT file with a xsl processor.

Log in to reply