How to get xml attributes with this syntax using Qt DOM

若如初见. 提交于 2019-12-08 02:18:39

问题


I am using Qt DOM XML parser and have run in to a problem with attributes defines like this:

<special-prop key="A">1</special-prop>

The two data I want from the above line is A and 1. I am able to get the 1, but not the key/value attribute pair.

My code and debug output below:

#include <QByteArray>
#include <QDebug>
#include <QDomDocument>
#include <QString>

int main(int argc, char *argv[])
{
    QString input("<special-prop key=\"A\">1</special-prop><special-prop key=\"B\">2</special-prop><special-prop key=\"C\">3</special-prop>");
    QByteArray bytes = input.toUtf8();

    QDomDocument doc;
    doc.setContent(bytes);

    QDomNodeList start = doc.elementsByTagName("special-prop");

    QDomNode node = start.item(0);
    qDebug() << "Element text: " << node.toElement().text();
    qDebug() << "Attributes found: " << node.hasAttributes();

    QDomNamedNodeMap map = node.attributes();
    qDebug() << "Attributes found: " << map.length();

    QDomAttr attr = node.toAttr();
    qDebug() << "Name: " << attr.name();
    qDebug() << "Value: " << attr.value();

    getchar();

    return 0;
}

The output looks like this:

Element text:  "1"
Attributes found:  true
Attributes found:  1
Name:  ""
Value:  ""

SOLUTION:

#include <QByteArray>
#include <QDebug>
#include <QDomDocument>
#include <QString>

int main(int argc, char *argv[])
{
    QString input("<special-prop key=\"A\">1</special-prop><special-prop key=\"B\">2</special-prop><special-prop key=\"C\">3</special-prop>");
    QByteArray bytes = input.toUtf8();

    QDomDocument doc;
    doc.setContent(bytes);

    QDomNodeList start = doc.elementsByTagName("special-prop");
    int length = start.length();
    int lengthInner;
    for (int i = 0; i < length; i++)
    {
        auto node = start.at(i);
        auto map = node.attributes();

        lengthInner = map.length();
        for (int j = 0; j < lengthInner; j++)
        {
            auto mapItem = map.item(j);
            auto attribute = mapItem.toAttr();
            qDebug() << "Name: " << attribute.name();
            qDebug() << "Value: " << attribute.value();
        }

        qDebug() << "Element text: " << node.toElement().text();
        qDebug() << "Attributes found: " << node.hasAttributes();
    }

    getchar();

    return 0;
}

Output:

Name:  "key"
Value:  "A"
Element text:  "1"
Attributes found:  true

回答1:


You need to iterate over the attribute map:

for (int i = 0; i < map.length(); ++i) {
   auto inode = map.item(i);
   auto attr = inode.toAttr();
   qDebug() << "Name: " << attr.name();
   qDebug() << "Value: " << attr.value();
}

The output is then as follows:

Element text:  "1"
Attributes found:  true
Attributes found:  1
Name:  "key"
Value:  "A"


来源:https://stackoverflow.com/questions/32082181/how-to-get-xml-attributes-with-this-syntax-using-qt-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!