[代码] 修改属性值
- bool QXmlEdit::SetAValue(const QString &key, const QVariant &value)
- {
- try
- {
- QString tag;
- QString attName;
- QStringList sl=key.split("/");
- tag = sl.at(0);
- attName = sl.at(1);
- QDomElement docElem = doc.documentElement();
- QDomNodeList nodeList = docElem.elementsByTagName(tag);
- if (nodeList.count() >0 )
- {
- QDomElement el = nodeList.at(0).toElement();
- el.setAttribute(attName,value.toString()); //设置属性值
- QFile file(fileName);
- if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
- {
- return false;
- }
- QTextStream out(&file);
- doc.save(out,4);
- }
- return true;
- }
- catch (...)
- {
- return false;
- }
- }
- bool QXmlEdit::SetTValue(const QString &key, const QVariant &value)
- {
- try
- {
- QDomElement docElem = doc.documentElement();
- QDomNodeList nodeList = docElem.elementsByTagName(key);
- if (nodeList.count() >0 )
- {
- QDomElement el = nodeList.at(0).toElement(); //tag为key的节点
- QDomNode oldnode = el.firstChild(); //标签之间的内容作为节点的子节点出现,得到原来的子节点
- el.firstChild().setNodeValue(value.toString()); //用提供的value值来设置子节点的内容
- QDomNode newnode = el.firstChild(); //值修改过后的子节点
- el.replaceChild(newnode,oldnode); //调用节点的replaceChild方法实现修改功能
- QFile file(fileName);
- if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
- {
- return false;
- }
- QTextStream out(&file);
- doc.save(out,4);
- }
- return true;
- }
- catch (...)
- {
- return false;
- }
- }