使用QDomDocument <r><a><a1><b>6</b><c>7</c><d id="1" name="55"><e name="33" va="2" /></d></a1></a></r>获取d的属性值
时间: 2024-02-22 08:53:06 浏览: 18
使用QDomDocument获取d的属性值的方法如下:
```cpp
#include <QDomDocument>
#include <QFile>
#include <QDebug>
int main()
{
QDomDocument doc;
QFile file("your_xml_file.xml"); // 替换为你的XML文件路径
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Failed to open the file.";
return -1;
}
if (!doc.setContent(&file))
{
qDebug() << "Failed to set content from the file.";
file.close();
return -1;
}
file.close();
QDomElement root = doc.documentElement();
QDomNodeList nodeList = root.elementsByTagName("d");
if (nodeList.count() > 0)
{
QDomElement dElement = nodeList.at(0).toElement();
QString id = dElement.attribute("id");
QString name = dElement.attribute("name");
qDebug() << "id: " << id;
qDebug() << "name: " << name;
}
else
{
qDebug() << "No 'd' element found.";
}
return 0;
}
```
请注意,上述代码中的"your_xml_file.xml"需要替换为你的XML文件的路径。该代码会打开XML文件,将其内容设置到QDomDocument中,然后通过根元素获取"d"元素,并获取其属性值。