如何使用Qt DOM使用此语法获取xml属性 [英] How to get xml attributes with this syntax using Qt DOM

查看:86
本文介绍了如何使用Qt DOM使用此语法获取xml属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Qt DOM XML解析器,并且遇到了这样的属性定义问题:

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>

上一行我想要的两个数据是A和1。我可以得到1 ,而不是键/值属性对。

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;
}

输出看起来像这样:

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

解决方案:

#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;
}

输出:

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


推荐答案

您需要遍历属性映射:

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();
}

输出如下:

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

这篇关于如何使用Qt DOM使用此语法获取xml属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆