TinyXML2 下载地fhg :
https://github.com/leethomason/tinyxml2
使用方法:
将.h.cpp 放至工程中,
然后 引用
#include"tinyxml2.h"
using namespace tinyxml2;
即可
<频率名称列表>
<测试>
<起始频率>555起始频率>
<终止频率>555终止频率>
<步进>555步进>
测试>
频率名称列表>
#pragma once
#include
#include"tinyxml2.h"
using namespace tinyxml2;
class FreqDataXml final
{
public:
FreqDataXml();
~FreqDataXml();
public:
void init();
void readXml();
XMLElement* queryrNodeByName(const QString& strNodeName);
bool insertNode(QString strName, float dS, float dE, float dStep);
bool modifyNode(XMLElement* pNode, QString strName, float dS, float dE, float dStep);
bool delNode(XMLElement* pNode);
bool getNodeFreqInfo(XMLElement* pNode, float& dS, float& dE, float& dStep);
void save();
private:
void createXml();
private:
QString m_strFileName;
XMLDocument m_doc;
XMLElement* m_pRoot = nullptr;
};
#include "FreqDataXml.h"
#include
#include
#include
FreqDataXml::FreqDataXml()
{
init();
}
FreqDataXml::~FreqDataXml()
{
//save();
}
void FreqDataXml::init()
{
m_strFileName = QCoreApplication::applicationDirPath() + "/freqdata.xml";
QFile file(m_strFileName);
if (!file.exists()) // 文件不存在
{
createXml();
}
else
{
tinyxml2::XMLError ret = m_doc.LoadFile(m_strFileName.toLocal8Bit().data());
if (XML_SUCCESS != ret)
{
// 文件错误, 删除
QFile fileTemp(m_strFileName);
fileTemp.remove();
m_doc.Clear();
return;
}
m_pRoot = m_doc.RootElement();
qDebug() << QString::fromLocal8Bit(m_pRoot->Name());
}
}
/* 创建后的文件如下:
< ? xml version = "1.0" encoding = "UTF-8" ? >
<频率名称列表>
< / 频率名称列表>
*/
void FreqDataXml::createXml()
{
const char* declaration = "";
m_doc.Parse(declaration);//会覆盖xml所有内容
m_pRoot = m_doc.NewElement("频率名称列表");
m_doc.InsertEndChild(m_pRoot);
//XMLElement* freq = m_doc.NewElement("航空");
//m_pRoot->InsertEndChild(freq);
//tinyxml2::XMLElement* freqS = m_doc.NewElement("起始频率");
//tinyxml2::XMLText* value = m_doc.NewText("68.9");
//freq->InsertEndChild(freqS);
//freqS->InsertEndChild(value);
//tinyxml2::XMLElement* freqE = m_doc.NewElement("终止频率");
//freq->InsertEndChild(freqE);
//value = m_doc.NewText("89.6");
//freqE->InsertEndChild(value);
//tinyxml2::XMLElement* freqStep = m_doc.NewElement("终止频率");
//freq->InsertEndChild(freqStep);
//value = m_doc.NewText("25");
//freqStep->InsertEndChild(value);
}
void FreqDataXml::readXml()
{
XMLElement* pNode = m_pRoot->FirstChildElement();
XMLElement* pSubNode = nullptr;
while (pNode)
{
// 频率名称
qDebug() << "\n";
const char* pName = pNode->Value();
qDebug() << QString::fromLocal8Bit(pName);
// 起始频率
pSubNode = pNode->FirstChildElement();
pName = pSubNode->Value();
qDebug() << QString::fromLocal8Bit(pName);
// 起始频率 值
pName = pSubNode->GetText();
qDebug() << QString::fromLocal8Bit(pName);
// 终止频率
pSubNode = pSubNode->NextSiblingElement();
pName = pSubNode->Value();
qDebug() << QString::fromLocal8Bit(pName);
// 终止频率 值
pName = pSubNode->GetText();
qDebug() << QString::fromLocal8Bit(pName);
// 步进
pSubNode = pSubNode->NextSiblingElement();
pName = pSubNode->Value();
qDebug() << QString::fromLocal8Bit(pName);
// 步进 值
pName = pSubNode->GetText();
qDebug() << QString::fromLocal8Bit(pName);
pNode = pNode->NextSiblingElement();
}
}
tinyxml2::XMLElement* FreqDataXml::queryrNodeByName(const QString& strNodeName)
{
XMLElement* pNode = m_pRoot->FirstChildElement();
XMLElement* pSubNode = nullptr;
while (pNode)
{
qDebug() << "\n";
const char* pName = pNode->Value();
if (0 == strNodeName.compare(QString::fromLocal8Bit(pName), Qt::CaseInsensitive))
return pNode;
pNode = pNode->NextSiblingElement();
}
return nullptr;
}
bool FreqDataXml::insertNode(QString strName, float dS, float dE, float dStep)
{
XMLElement* nodeName = m_doc.NewElement(strName.toLocal8Bit().constData());
m_pRoot->InsertEndChild(nodeName);
XMLElement* nodeStartFreq = m_doc.NewElement("起始频率");
nodeStartFreq->SetText(dS);
nodeName->InsertEndChild(nodeStartFreq);
XMLElement* nodeEndFreq = m_doc.NewElement("终止频率");
nodeEndFreq->SetText(dE);
nodeName->InsertEndChild(nodeEndFreq);
XMLElement* nodeStepFreq = m_doc.NewElement("步进");
nodeStepFreq->SetText(dStep);
nodeName->InsertEndChild(nodeStepFreq);
return true;
}
bool FreqDataXml::modifyNode(XMLElement* pNode, QString strName, float dS, float dE, float dStep)
{
if (!pNode)
return false;
pNode->SetName(strName.toLocal8Bit().constData());
//pNode->SetText(strName.toLocal8Bit().constData());
pNode = pNode->FirstChildElement();
pNode->SetText(dS);
pNode = pNode->NextSiblingElement();
pNode->SetText(dE);
pNode = pNode->NextSiblingElement();
pNode->SetText(dStep);
}
bool FreqDataXml::delNode(XMLElement* pNode)
{
if (!pNode)
return false;
m_pRoot->DeleteChild(pNode);
return true;
}
bool FreqDataXml::getNodeFreqInfo(XMLElement* pNode, float& dS, float& dE, float& dStep)
{
if (!pNode)
return false;
pNode = pNode->FirstChildElement();
dS = atof(pNode->GetText());
pNode = pNode->NextSiblingElement();
dE = atof(pNode->GetText());
pNode = pNode->NextSiblingElement();
dStep = atof(pNode->GetText());
return true;
}
void FreqDataXml::save()
{
m_doc.SaveFile(m_strFileName.toLocal8Bit().data());
}
FreqDataXml freq;
freq.insertNode(QString::fromLocal8Bit("如同太陽"), 33.2f, 33.3f, 22.2f);
freq.insertNode(QString::fromLocal8Bit("定文件"), 3254.99f, 232.52f, 54.6f);
freq.insertNode(QString::fromLocal8Bit("删除"), 54.6f, 335.3f, 222.2f);
freq.insertNode(QString::fromLocal8Bit("源码库"), 324.2f, 83.3f, 12.2f);
freq.readXml();
XMLElement* pNode = freq.queryrNodeByName(QString::fromLocal8Bit("删除"));
float dS;
float dE;
float dStep;
freq.getNodeFreqInfo(pNode, dS, dE, dStep);
qDebug() << dS << "\t" << dE << "\t" << dStep;
pNode = freq.queryrNodeByName(QString::fromLocal8Bit("删除"));
freq.modifyNode(pNode, QString::fromLocal8Bit("删除x"), 11.1f, 22.2f, 33.3f);
pNode = freq.queryrNodeByName(QString::fromLocal8Bit("删除"));
freq.getNodeFreqInfo(pNode, dS, dE, dStep);
qDebug() << dS << "\t" << dE << "\t" << dStep;
freq.save();
```