A question about Reading an XML file in Qt

  • 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 -->
    <Tree_1 bark="mossy" leaves="none" nest="lots"/>
    <!-- Legally distinguable from Bambi -->

  • 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.

  • 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);
    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

  • 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?

  • 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.

    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.

  • 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

    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

  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.