1.写xml文件
QFile file(m_pXMLPath);
if(!file.open(QFile::WriteOnly))
return;
QDomDocument doc;
QDomProcessingInstruction st;
st = doc.createProcessingInstruction("xml", "version=\1.0", encoding =\"utf-8\"");
doc.appendChild(st);
QDomElement root = doc.createElement("HardWareType");
doc.appendChild(root);
QDomElement node = doc.createElement("MachineType");
QDomElement nodeChild = doc.createElement("Machine");
nodeChild.setAttribute("Type", "test");
nodeChild.setAttribute("Command", "command");
node.appendChild(nodeChild);
root.appendChild(node);
QDomElement cameraNode = doc.createElement("CameraType");
QDomElement cameraNodeChild = doc.createElement("Camera");
cameraNodeChild.setAttribute("Type", "testCamera");
cameraNodeChild.setAttribute("Command", "commandCamera");
cameraNode.appendChild(cameraNodeChild);
root.appendChild(cameraNode);
QTextStream stream(&file);
doc.save(stream, 4);
file.close();
结果如下:
<?xml version='1.0'?>
<HardWareType>
<MachineType>
<Machine Command="command" Type="test"/>
</MachineType>
<CameraType>
<Camera Command="commandCamera" Type="testCamera"/>
</CameraType>
</HardWareType>
2、读xml有两种方式,我用的是比较好理解的一种方式
QFile file(“./test.xml”);
if(!file.open(QFile::ReadOnly | QFile::Text))
return;
//将文件给xml读文件流
QXmlStreamReader xmlStream(&file);
while(!xmlStream.atEnd() && !xmlStream.hasError())
{
//获取下个节点属性
QXmlStreamReader::TokenType read = xmlStream.readNext();
if(read == QXmlStreamReader::StartDocument)
continue;
if(read == QXmlStreamReader::StartElement)
{
QXmlStreamAttributes attr = xmlStream.attributes();
QString type = attr.value("Type").toString();
QString command = attr.value("Command").toString();
//根据类型存入容器中
if(xmlStream.name() == "Machine")
m_machineMap.insert(type, command);
else if(xmlStream.name() == "Camera")
m_cameraMap.insert(type, command);
}
}
file.close();
3.增加xml内容
QFile file(“./test.xml”);
if(!file.open(QFile::ReadOnly))
return;
QDomDocument doc;
//将file给doc,进行操作
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
//获取根节点
QDomElement root = doc.documentElement();
//type是我增加代码中区分机器和相机的
if(type == HardWare_Machine)
{
//获取MachineType节点
QDomNodeList list = root.elementsByTagName("MachineType");
//向MachineType节点增加元素
QDomElement addEl = doc.createElement("Machine");
addEl.setAttribute("Type", “test2”);
addEl.setAttribute("Command", “command2”);
list.at(0).appendChild(addEl);
}
else if(type == HardWare_Camera)
{
//获取CameraType节点
QDomNodeList list = root.elementsByTagName("CameraType");
//向CameraType节点增加元素
QDomElement addEl = doc.createElement("Camera");
addEl.setAttribute("Type", "testCamera1");
addEl.setAttribute("Command", "commandCamera1");
list.at(0).appendChild(addEl);
}
//添加完元素,写入文件中
if(file.open(QFile::WriteOnly | QFile::Truncate))
{
QTextStream textStream(&file);
doc.save(textStream, 4);
}
file.close();
添加结果如下:
<?xml version='1.0'?>
<HardWareType>
<MachineType>
<Machine Command="command" Type="test"/>
<Machine Command="command2" Type="test2"/>
</MachineType>
<CameraType>
<Camera Command="commandCamera" Type="testCamera"/>
<Camera Command="commandCamera1" Type="testCamera1"/>
</CameraType>
</HardWareType>
Qtxml文件操作(写,读,添加)
于 2021-05-11 11:43:21 首次发布