void DomXml::parseXml()
{
//QDomDocument document; 声明QDomDocument
//(document.setContent(&xml) 初始化QDomDocument 文档结构
//QDomElement root = document.documentElement(); 返回文档的根节点
//QDomElement element = root.firstChildElement(); 第二层节点
//while (!element.isNull()) 判断节点是否为空
//QString tag_name = element.attributeNode("name").value(); 获取节点值
//element = element.nextSiblingElement(); 遍历下一节点
QFile xml("D:\\city.xml");
if (xml.open(QFile::ReadOnly | QFile::Text))
{
QDomDocument document;
//bool QDomDocument::setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg = Q_NULLPTR, int *errorLine = Q_NULLPTR, int *errorColumn = Q_NULLPTR)
if (document.setContent(&xml))
{
QDomElement root = document.documentElement();
QString root_tag_name = root.tagName();
if (root.hasAttribute("name"))
{
QString name = root.attributeNode("name").value();
mode->setHorizontalHeaderItem(0, new QStandardItem(name));
QDomElement element = root.firstChildElement();
while (!element.isNull())
{
QString tag_name = element.attributeNode("name").value();
QStandardItem * province_Item = new QStandardItem(tag_name);
mode->appendRow(province_Item); //省
QDomElement city = element.firstChildElement();
while (city.hasAttribute("name"))
{
QString tag_name = city.attributeNode("name").value();
QStandardItem * cityItem = new QStandardItem(tag_name);
province_Item->appendRow(cityItem); //市
QDomElement aera = city.firstChildElement();
while (aera.hasAttribute("name"))
{
QString tag_name = aera.attributeNode("name").value();
QStandardItem * aeraItem = new QStandardItem(tag_name);
cityItem->appendRow(aeraItem); //县区
aera = aera.nextSiblingElement();
}
city = city.nextSiblingElement();
}
element = element.nextSiblingElement();
}
}
}
}
}