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. A question about Reading an XML file in Qt
Forum Updated to NodeBB v4.3 + New Features

A question about Reading an XML file in Qt

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 5.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.
  • A Offline
    A Offline
    Arukas
    wrote on last edited by
    #1

    I am using the qt xml streaming reader class. I should point out, I am trying to write a dynamic reader. I want to change the xml file without having to recode the reader part of my program. There's a line, in my xml file, with "Tree_1", I am having trouble reading the attributes without knowing them first. The given attributes function works with only knowing the name of the attribute work. I'd like to be able to add an xml line like:

    <Tree_2 size="small' type="cherry tree" status="George is chopping it down"/>

    I'd like my program to be able to return the types number of attributes with their names. Is this possible?

    @<?xml verision="1.0"?>
    <!-- Space for Rent -->
    <Forest>
    <Number_of_Trees>200</Trees>
    <Tree_1 bark="mossy" leaves="none" nest="lots"/>
    <Animals>
    <!-- Legally distinguable from Bambi -->
    <Rabbit>Thumperette</Rabbit>
    <Skunk>Rose</Skunk>
    </Animals>
    </Forest>@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      QXmlStreamReader::attributes() gives you back a QXmlStreamAttributes, which is a QVector of QXmlStreamAttribute objects. You can iterate over that, the attribute object has a name, value and some more getters.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Arukas
        wrote on last edited by
        #3

        Now I am having a problem doing that. I am having a problem getting it to work. I'm not doing something write in Qt, and I think it might be the root of my confusion.

        Here's is my code snippet I am using.

        @QXmlStreamReader reader(&xmlFile);
        while(!reader.isEndDocument())
        {
        reader.readNext();
        if (reader.isStartElement())
        {
        if (reader.attributes().size() >= 1 )
        {
        cout << reader.attributes().value(0).value.toString().toStdString();
        }
        }
        }@

        Shouldn't this be working?

        edit: Minor Code Edit -Arukas

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Arukas
          wrote on last edited by
          #4

          I got something to work.

          @cout << reader.attributes().at(0).name().toString().toStdString() << endl;@

          However, Qt seems to be real cranky with me. Why doesn't anything appear after the at(0) when I type "." or ctrl+space?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Why do yo look only at the first attribute? The tag may contain more than one!

            To show all attributes and values use this code:

            @
            QXmlStreamAttributes allAttributs = reader.attributes();
            foreach(const QXmlStreamAttribute &att, allAttributes) {
            qDebug() << "attribute" << att.name() << "=" << att.value();
            }
            @

            The code completion works for me.

            BTW:
            you should have a look at [[Doc:QDebug]], it has stream operators like cout, but supports pretty formatting of many Qt classes, you rarely need a toString() to get reasonable output.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Arukas
              wrote on last edited by
              #6

              Originally, I was only looking to get something to work. I actually use a good ol'fashion for loop to do it. Haven't even heard of a foreach, until you mentioned it. Looks pretty handy. Everything is working nicely, now. Also, thanks for explaining QDebug, I've been wondering what its purpose was, just hadn't asked yet.

              Although I want to know something particular about Qt. Why could I not use the chain sort cuts after
              @reader.attributes().at(0)@

              I had to manually type everything in after the "at(0)". Why did I have to do that, instead of getting those quick pick options? Second chaining didn't seem to work in some of the cases.

              You have had a better handle on the issue, and you would not of had that problem. I'd like to learn why my approach wasn't working, so I can have a better handle in the future.

              edit: Forgot to proof Read-Arukas

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                Sorry, I don't have a clue why the auto completion doesn't work. I'd suggest to start a separate thread in the Tools forum, where the Qt Creator guys may catch up.

                http://www.catb.org/~esr/faqs/smart-questions.html

                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