0
点赞
收藏
分享

微信扫一扫

HTTP/HTTPS ②-Cookie || Session || HTTP报头

晴儿成长记 01-08 12:00 阅读 11

使用单例类模板实现的对XML文件的节点、属性、文本进行增删改查,可以直接用!
直接POST代码,比较简单好用。
针对以下格式的xml文件比较适用
每个节点的名称都不一样,节点包含了各种属性。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <Param1 Icon="set_1.ico" name="参数设置1">
        <!-- 项目名称,文本类型 -->
        <Node_0 default_value="default_value1" zh-cn="节点0" control_type="LineEdit">设置值1</Node_0>
        <Node_1 default_value="default_value2" zh-cn="节点1" control_type="LineEdit">设置值2</Node_1>
        <Node_2 default_value="default_value3" zh-cn="节点2" control_type="LineEdit">设置值3</Node_2>
    </Param1>
    <Param2 Icon="set_2.ico" name="参数设置2">
        <Node_3 default_value="Item1;Item2;Item3" zh-cn="节点3" control_type="ComboBox">TCPClient</Node_3>
        <Node_4 default_value="true" zh-cn="节点4" control_type="CheckBox">false</Node_4>
    </Param2>
</configuration>

头文件

#pragma once
#include "SingletonCRTP.h"
#include <stdio.h>
#include <iostream>
#include <QObject>
#include <QXmlStreamReader>
#include <QFile>
#include <QtXml\QDomComment>
#include <QDir>
#include <QTextStream>
#include <QCoreApplication>

class XmlHelper: public SingletonCRTP<XmlHelper>
{
	friend class SingletonCRTP<XmlHelper>;


public:
    explicit XmlHelper(const QString& xmlFilePath);
    XmlHelper() {};
    bool loadXml(const QString& filePath);

    QString getNode(const QString& nodeName) const;
    QStringList getNodes(const QString& nodeName) const;
    QStringList getChildNodes(const QString& nodeName) const;

    QStringList getNodes(const QString& nodeName, const QString& attrName) const;
    QString getAttribute(const QString& nodeName, const QString& attrName) const;
    QStringList getAttributes(const QString& nodeName, const QString& attrName) const;
    QString getText(const QString& nodeName) const;
    bool setNodeText(const QString& nodeName, const QString& text);
    bool setNodeAttribute(const QString& nodeName, const QString& attrName, const QString& attrValue);
    bool addNode(const QString& nodeName, const QString& text);
    bool removeNode(const QString& nodeName);
    bool saveToFile(const QString& filePath);
    bool save();

private:
    QDomDocument xmldoc;
    QFile xmlFile;
    QDomNode findFirstNode(const QString& nodeName) const;

};

源文件

#include "XmlHelper.h"
#include <QDomElement>
#include <QDomNodeList>
#include <QXmlStreamReader>

XmlHelper::XmlHelper(const QString& xmlFilePath)
{
	loadXml(xmlFilePath);
}


bool XmlHelper::loadXml(const QString& filePath)
{
	xmlFile.setFileName(filePath);
	if (!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		return false;
	}

	QXmlStreamReader xmlReader(&xmlFile);
	if (!xmldoc.setContent(&xmlReader, true))
	{
		xmlFile.close();
		return false;
	}
	xmlFile.close();


	return true;
}

QDomNode XmlHelper::findFirstNode(const QString& nodeName) const
{
	QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
	if (!nodeList.isEmpty())
	{
		return nodeList.at(0);
	}
	return QDomNode();
}

QString XmlHelper::getNode(const QString& nodeName) const
{
	QDomNode node = findFirstNode(nodeName);
	if (!node.isNull())
	{
		QDomElement element = node.toElement();
		return element.tagName();
	}
	return QString();
}

QStringList XmlHelper::getNodes(const QString& nodeName) const
{
	QStringList nodeNames;
	QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
	for (int i = 0; i < nodeList.size(); ++i)
	{
		QDomElement element = nodeList.at(i).toElement();
		nodeNames.append(element.tagName());
	}
	return nodeNames;
}

QStringList XmlHelper::getChildNodes(const QString& nodeName) const
{
	QStringList nodeNames;
	QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
	for (int i = 0; i < nodeList.size(); ++i)
	{
		QDomElement element = nodeList.at(i).toElement();
		QDomNodeList childs = element.childNodes();
		for (int j = 0; j < childs.size(); ++j)
		{
			if (childs.at(j).toElement().tagName() != "")
			{
				nodeNames.append(childs.at(j).toElement().tagName());
			}
		}
	}
	return nodeNames;
}

QStringList XmlHelper::getNodes(const QString& nodeName, const QString& attrName) const
{
	QStringList nodeNames;
	QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
	for (int i = 0; i < nodeList.size(); ++i)
	{
		QDomElement element = nodeList.at(i).toElement();
		if (element.attribute("name") == attrName)
		{
			nodeNames.append(element.tagName());
		}
	}
	return nodeNames;
}

QString XmlHelper::getAttribute(const QString& nodeName, const QString& attrName) const
{
	QDomNode node = findFirstNode(nodeName);
	if (!node.isNull())
	{
		QDomElement element = node.toElement();
		return element.attribute(attrName);
	}
	return QString();
}

QStringList XmlHelper::getAttributes(const QString& nodeName, const QString& attrName) const
{
	QStringList attrValues;
	QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
	for (int i = 0; i < nodeList.size(); ++i)
	{
		QDomElement element = nodeList.at(i).toElement();
		if (element.hasAttribute(attrName))
		{
			attrValues.append(element.attribute(attrName));
		}
	}
	return attrValues;
}

QString XmlHelper::getText(const QString& nodeName) const
{
	QDomNode node = findFirstNode(nodeName);
	if (!node.isNull())
	{
		QDomElement element = node.toElement();
		return element.text();
	}
	return QString();
}


bool XmlHelper::setNodeText(const QString& nodeName, const QString& text)
{
	QDomNode node = findFirstNode(nodeName);
	if (!node.isNull())
	{
		QDomNode oldnode = node.firstChild();
		node.firstChild().setNodeValue(text);
		QDomNode newnode = node.firstChild();     //值修改过后
		node.replaceChild(newnode, oldnode);      //调用节点的replaceChild方法实现修改功能

		return true;
	}
	return false;
}

bool XmlHelper::setNodeAttribute(const QString& nodeName, const QString& attrName, const QString& attrValue)
{
	QDomNode node = findFirstNode(nodeName);
	if (!node.isNull())
	{
		QDomElement element = node.toElement();
		element.setAttribute(attrName, attrValue);
		return true;
	}
	return false;
}

bool XmlHelper::addNode(const QString& nodeName, const QString& text)
{
	QDomElement root = xmldoc.documentElement();
	QDomElement newNode = xmldoc.createElement(nodeName);
	newNode.appendChild(xmldoc.createTextNode(text));
	root.appendChild(newNode);
	return true;
}

bool XmlHelper::removeNode(const QString& nodeName)
{
	QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
	for (int i = 0; i < nodeList.size(); ++i)
	{
		QDomNode node = nodeList.at(i);
		node.parentNode().removeChild(node);
	}
	return true;
}

bool XmlHelper::saveToFile(const QString& filePath)
{
	QFile file(filePath);

	if (!file.open(QFile::WriteOnly | QFile::Truncate | QFile::Text))
	{
		return false;
	}
	try
	{
		QTextStream stream(&file);
		stream.setCodec("utf-8");
		xmldoc.save(stream, 4, QDomNode::EncodingFromTextStream);
		file.close();
		return true;
	}
	catch (const std::exception&)
	{
		return false;
	}
}

bool XmlHelper::save()
{
	if (!xmlFile.open(QFile::WriteOnly | QFile::Truncate | QFile::Text))
	{
		return false;
	}
	try
	{
		QTextStream stream(&xmlFile);
		stream.setCodec("utf-8");
		xmldoc.save(stream, 4, QDomNode::EncodingFromTextStream);
		xmlFile.close();
		return true;
	}
	catch (const std::exception& e)
	{
		std::cout << e.what() << std::endl;
		return false;
	}
}


举报

相关推荐

0 条评论