这是我的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 >
我能够完全获得Id
,Translation
和English
的值,但我无法获得EnglishName
,CultureName
的值和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;
}
}
我将EnglishName
,CultureName
和Culture
视为空白。我的错误在哪里?
答案 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;
}
}
我已经在这里测试了你的文件并且它有效。