使用QDomNodeList从Qt中的xml文件中读取属性名称及其值

时间:2015-04-21 09:51:21

标签: xml qt

这是我的xml文件:

<Dictionary EnglishName="English (United States)" CultureName="English (United States)" Culture="en-US">
    <Value Id="ID_OK" Translation="OK" English="OK" />
    <Value Id="ID_Cancel" Translation="Cancel" English="Cancel1" />
    <Value Id="ID_DemoMode" Translation="Demo Mode" English="Demo Mode" />
    <Value Id="ID_PhoneNumberColon" Translation="Phone Number:" English="Phone Number:" />
    <Value Id="ID_FaxNumberColon" Translation="Fax Number:" English="Fax Number:" />
    <Value Id="ID_CustomerColon" Translation="Customer:" English="Customer:" />
    <Value Id="ID_CompanyColon" Translation="Company:" English="Company:" />
</Dictionary >

我能够完全获得IdTranslationEnglish的值,但我无法获得EnglishNameCultureName的值和Culture。 这是我的代码示例:

QDomDocument doc("Labguage");
QFile file("English.xml");
if (!file.open(QIODevice::ReadOnly))
    return;

if (!doc.setContent(&file)) {
    file.close();
    return;
}

file.close();

QDomNode root = doc.namedItem("Dictionary");
QDomNodeList nodeList = root.childNodes();
if (nodeList.count() > 0) {
    for(int iDx = 0;iDx < nodeList.count(); iDx++) {
        QString EnglishName(nodeList.at(iDx).attributes().namedItem("EnglishName").nodeValue());
        QString CultureName(nodeList.at(iDx).attributes().namedItem("CultureName").nodeValue());
        QString Culture(nodeList.at(iDx).toElement().attribute("Culture"));
        QString Translation(nodeList.at(iDx).attributes().namedItem("Translation").nodeValue());
        QString English(nodeList.at(iDx).attributes().namedItem("English").nodeValue());
        QString Id(nodeList.at(iDx).toElement().attribute("Id"));
        qDebug() << "Id: " << Id<<endl;
        qDebug()<<"English::"<<English<<endl;
        qDebug()<<"Translation::"<<Translation<<endl;
        qDebug() << "EnglishName: " <<EnglishName<<endl;
        qDebug()<<"CultureName:"<<CultureName<<endl;
        qDebug()<<"Culture:"<<Culture<<endl;
    }
}

我将EnglishNameCultureNameCulture视为空白。我的错误在哪里?

1 个答案:

答案 0 :(得分:1)

EnglishName,CultureName和Culture是根节点的属性,请参考以下代码:

QDomDocument doc("Labguage");
QFile file("English.xml");
if (!file.open(QIODevice::ReadOnly))
    return -1;

if (!doc.setContent(&file)) {
    file.close();
    return -1;
}

file.close();

QDomNode root = doc.namedItem("Dictionary");
qDebug() << root.attributes().count();
qDebug() << root.attributes().namedItem("EnglishName").nodeValue();
qDebug() << root.attributes().namedItem("CultureName").nodeValue();
qDebug() << root.attributes().namedItem("Culture").nodeValue();
QDomNodeList nodeList = root.childNodes();
if (nodeList.count() > 0) {
    for(int iDx = 0;iDx < nodeList.count(); iDx++) {
        QString EnglishName(nodeList.at(iDx).attributes().namedItem("EnglishName").nodeValue());
        QString CultureName(nodeList.at(iDx).attributes().namedItem("CultureName").nodeValue());
        QString Culture(nodeList.at(iDx).toElement().attribute("Culture"));
        QString Translation(nodeList.at(iDx).attributes().namedItem("Translation").nodeValue());
        QString English(nodeList.at(iDx).attributes().namedItem("English").nodeValue());
        QString Id(nodeList.at(iDx).toElement().attribute("Id"));
        qDebug() << "Id: " << Id<<endl;
        qDebug()<<"English::"<<English<<endl;
        qDebug()<<"Translation::"<<Translation<<endl;
        qDebug() << "EnglishName: " <<EnglishName<<endl;
        qDebug()<<"CultureName:"<<CultureName<<endl;
        qDebug()<<"Culture:"<<Culture<<endl;
    }
}

我已经在这里测试了你的文件并且它有效。