qt将xml文件写到html文件,Qt读写xml文件
写xml//添加xml说明QDomDocument doc;QDomProcessingInstruction instru;instru = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");doc.appendChild(instru);//添加根节点QDomElement root = d
写xml
//添加xml说明
QDomDocument doc;
QDomProcessingInstruction instru;
instru = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instru);
//添加根节点
QDomElement root = doc.createElement("root");
doc.appendChild(root);
root.setAttribute("ver", "1.0.0");
//添加元素
QDomElement elementNode= doc.createElement(" element");
QDomElement subNode= doc.createElement("sub");
subNode.setAttribute("id", "-1");
elementNode.appendChild(subNode);
root.appendChild(elementNode);
//写文件
QFile file(fileName);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream out(&file);
doc.save(out, 4);
file.close();
}
读xml
QDomDocument doc;
//读取xml文件到QDomDocument 对象中
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) return false;
if (!doc.setContent(&file)){
file.close();
return false;
}
file.close();
//找到对应节点
QDomElement rootEle = doc.documentElement();
if ("root" != rootEle.nodeName()) return false;
if ("1.0.0" != rootEle.attribute("ver")) return false;
QDomNodeList subList= doc.elementsByTagName("sub");
for (int index = 0; index != subList.size(); ++index){
QDomNode node = subList.at(index);
if (!node.isElement()) continue;
QDomElement subEle= node.toElement();
QString id= subEle.attribute("id");
}
已经工作的程序员朋友可以关注下我的gzh“程序员成长日志”,分享日常工作中解决的问题即可赚取稿费,大家一起成长~
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)