Qt 笔记: Dom XML

首先确保已经在.pro文件中加载dom模块

 

QT += xml            //加入xml模块

 

 

1> 创建xml文件,并写入.xml文件中

 

QDomDocument xmlDoc; //创建xml对象。

QDomElement  rootElement = xmlDoc.createElement("root");// 由xmlDoc创建新的节点,所有的节点都有xmlDoc创建

 

QDomNode tmpNode = xmlDoc.createElement("node1"); //创建节点

tmpNode.toElement().setAttribute("value", "node1");// 节点属性

rootElement.appendChild(tmpNode); //将节点添加到根节点

 

..... // 多个节点

 

 

xmlDoc.appendChild(rootElement);

 QDomNode xmlNode = xmlDoc.createProcessingInstruction("xml", "version=/"1.0/" encoding=/"ISO-8859-1/""); //创建<xml>节点,设置xml文件页面解码,可以防止页面出现乱码
 xmlDoc.insertBefore(xmlNode, rootElement); //save xml copy files

QFile xmlFile("c://temp//xml.xml");

 

xmlFile.open(QIODevice::readOnly);

 

QTextStream ts(&xmlFile);

xmlDoc.save(ts, 4);

xmlFile.close();

 

==================================================================

待续

你可能感兴趣的:(c,xml,qt,encoding)