使用 Qt C++ 解析和处理 XML 文件
以下是使用 Qt C++ 实现 XML 文件处理的几种方法,包括解析、创建和修改 XML 文件。
1. 使用 QXmlStreamReader (推荐方式)
#include <QFile>
#include <QXmlStreamReader>
#include <QDebug>
void parseXmlWithStream(const QString &filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "无法打开文件:" << filePath;
return;
}
QXmlStreamReader xml(&file);
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
qDebug() << "开始元素:" << xml.name();
// 打印所有属性
for (const auto &attr : xml.attributes()) {
qDebug() << " 属性:" << attr.name() << "=" << attr.value();
}
} else if (xml.isEndElement()) {
qDebug() << "结束元素:" << xml.name();
} else if (xml.isCharacters() && !xml.isWhitespace()) {
qDebug() << "文本内容:" << xml.text().trimmed();
}
}
if (xml.hasError()) {
qDebug() << "XML 解析错误:" << xml.errorString();
}
file.close();
}
// 使用示例
int main() {
parseXmlWithStream("example.xml");
return 0;
}
2. 使用 QDomDocument (DOM 方式)
#include <QDomDocument>
#include <QFile>
#include <QDebug>
void parseXmlWithDom(const QString &filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "无法打开文件:" << filePath;
return;
}
QDomDocument doc;
if (!doc.setContent(&file)) {
qDebug() << "无法解析 XML 文件";
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
qDebug() << "根元素:" << root.tagName();
// 递归遍历所有节点
std::function<void(const QDomNode &, int)> traverseNode =
[&traverseNode](const QDomNode &node, int indent) {
QDomNode n = node;
while (!n.isNull()) {
qDebug().noquote() << QString(indent, ' ') << "节点:" << n.nodeName();
if (n.isElement()) {
QDomElement e = n.toElement();
if (e.hasAttributes()) {
QDomNamedNodeMap attrs = e.attributes();
for (int i = 0; i < attrs.length(); ++i) {
QDomNode attr = attrs.item(i);
qDebug().noquote() << QString(indent+2, ' ')
<< "属性:" << attr.nodeName()
<< "=" << attr.nodeValue();
}
}
}
if (n.hasChildNodes()) {
QDomNode child = n.firstChild();
while (!child.isNull()) {
if (child.isText()) {
QString text = child.nodeValue().trimmed();
if (!text.isEmpty()) {
qDebug().noquote() << QString(indent+2, ' ')
<< "文本:" << text;
}
} else {
traverseNode(child, indent+2);
}
child = child.nextSibling();
}
}
n = n.nextSibling();
}
};
traverseNode(root.firstChild(), 0);
}
// 使用示例
int main() {
parseXmlWithDom("example.xml");
return 0;
}
3. 解析 Qt UI 文件示例
#include <QDomDocument>
#include <QFile>
#include <QDebug>
void parseUiFile(const QString &uiFile) {
QFile file(uiFile);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "无法打开 UI 文件:" << uiFile;
return;
}
QDomDocument doc;
if (!doc.setContent(&file)) {
qDebug() << "无法解析 UI 文件";
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
// 查找所有 widget 元素
QDomNodeList widgets = root.elementsByTagName("widget");
qDebug() << "找到" << widgets.count() << "个 widget";
for (int i = 0; i < widgets.count(); ++i) {
QDomElement widget = widgets.at(i).toElement();
QString name = widget.attribute("name");
QString className = widget.attribute("class");
qDebug() << "Widget" << i+1 << ":" << name << "(" << className << ")";
}
// 查找所有连接
QDomNodeList connections = root.elementsByTagName("connection");
qDebug() << "\n找到" << connections.count() << "个连接";
for (int i = 0; i < connections.count(); ++i) {
QDomElement connection = connections.at(i).toElement();
QString sender = connection.firstChildElement("sender").text();
QString signal = connection.firstChildElement("signal").text();
QString receiver = connection.firstChildElement("receiver").text();
QString slot = connection.firstChildElement("slot").text();
qDebug() << "连接" << i+1 << ":" << sender << "." << signal
<< "->" << receiver << "." << slot;
}
}
// 使用示例
int main() {
parseUiFile("mainwindow.ui");
return 0;
}
4. 创建 Qt XML 文件
#include <QDomDocument>
#include <QFile>
#include <QTextStream>
#include <QDebug>
void createQtXml(const QString &outputFile) {
QDomDocument doc("QtConfig");
// 创建处理指令
QDomProcessingInstruction instr = doc.createProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instr);
// 创建根元素
QDomElement root = doc.createElement("QtSettings");
root.setAttribute("version", "1.0");
doc.appendChild(root);
// 添加窗口设置
QDomElement window = doc.createElement("Window");
window.setAttribute("name", "MainWindow");
root.appendChild(window);
QDomElement width = doc.createElement("Width");
width.appendChild(doc.createTextNode("800"));
window.appendChild(width);
QDomElement height = doc.createElement("Height");
height.appendChild(doc.createTextNode("600"));
window.appendChild(height);
// 添加按钮设置
QDomElement button = doc.createElement("Button");
button.setAttribute("name", "btnOK");
root.appendChild(button);
QDomElement text = doc.createElement("Text");
text.appendChild(doc.createTextNode("OK"));
button.appendChild(text);
// 保存到文件
QFile file(outputFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "无法写入文件:" << outputFile;
return;
}
QTextStream stream(&file);
doc.save(stream, 2); // 2 表示缩进空格数
file.close();
qDebug() << "已创建 Qt XML 文件:" << outputFile;
}
// 使用示例
int main() {
createQtXml("qt_config.xml");
return 0;
}
5. 修改 Qt XML 文件
#include <QDomDocument>
#include <QFile>
#include <QTextStream>
#include <QDebug>
void modifyQtXml(const QString &inputFile, const QString &outputFile) {
QFile file(inputFile);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "无法打开文件:" << inputFile;
return;
}
QDomDocument doc;
if (!doc.setContent(&file)) {
qDebug() << "无法解析 XML 文件";
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
// 修改所有 Button 元素的文本
QDomNodeList buttons = root.elementsByTagName("Button");
for (int i = 0; i < buttons.count(); ++i) {
QDomElement button = buttons.at(i).toElement();
QDomElement textElem = button.firstChildElement("Text");
if (!textElem.isNull()) {
QDomNode textNode = textElem.firstChild();
textNode.setNodeValue("New Text");
}
}
// 添加新元素
QDomElement newButton = doc.createElement("Button");
newButton.setAttribute("name", "btnCancel");
QDomElement newText = doc.createElement("Text");
newText.appendChild(doc.createTextNode("Cancel"));
newButton.appendChild(newText);
root.appendChild(newButton);
// 保存修改
QFile outFile(outputFile);
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "无法写入文件:" << outputFile;
return;
}
QTextStream stream(&outFile);
doc.save(stream, 2);
outFile.close();
qDebug() << "已修改并保存到:" << outputFile;
}
// 使用示例
int main() {
modifyQtXml("qt_config.xml", "qt_config_modified.xml");
return 0;
}
6. 使用 SAX 解析器 (QXmlSimpleReader)
#include <QXmlSimpleReader>
#include <QXmlInputSource>
#include <QFile>
#include <QDebug>
class MyXmlHandler : public QXmlDefaultHandler {
public:
bool startElement(const QString &namespaceURI,
const QString &localName,
const QString &qName,
const QXmlAttributes &attributes) override {
qDebug() << "开始元素:" << qName;
for (int i = 0; i < attributes.count(); ++i) {
qDebug() << " 属性:" << attributes.qName(i)
<< "=" << attributes.value(i);
}
return true;
}
bool endElement(const QString &namespaceURI,
const QString &localName,
const QString &qName) override {
qDebug() << "结束元素:" << qName;
return true;
}
bool characters(const QString &ch) override {
QString text = ch.trimmed();
if (!text.isEmpty()) {
qDebug() << "文本内容:" << text;
}
return true;
}
bool fatalError(const QXmlParseException &exception) override {
qDebug() << "XML 解析错误:" << exception.message();
return false;
}
};
void parseXmlWithSax(const QString &filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "无法打开文件:" << filePath;
return;
}
QXmlSimpleReader reader;
MyXmlHandler handler;
reader.setContentHandler(&handler);
reader.setErrorHandler(&handler);
QXmlInputSource source(&file);
if (!reader.parse(&source)) {
qDebug() << "解析失败";
}
file.close();
}
// 使用示例
int main() {
parseXmlWithSax("example.xml");
return 0;
}
总结
- QXmlStreamReader - 流式解析器,内存效率高,适合大文件
- QDomDocument - DOM 解析器,适合小文件,便于随机访问和修改
- SAX 解析器 - 事件驱动模型,适合只需读取不需要修改的情况
- 对于 Qt UI 文件,可以使用上述任意方法解析
选择建议:
- 大文件或只需顺序读取 → QXmlStreamReader
- 需要频繁修改或随机访问 → QDomDocument
- 特殊需求或性能关键 → SAX 解析器
from PyQt5.QtCore import QFile, QIODevice, QXmlStreamReader
def parse_xml_with_stream(file_path):
file = QFile(file_path)
if not file.open(QIODevice.ReadOnly | QIODevice.Text):
print(f"无法打开文件: {file_path}")
return
xml = QXmlStreamReader(file)
while not xml.atEnd():
xml.readNext()
if xml.isStartElement():
print(f"开始元素: {xml.name()}")
# 打印所有属性
for attr in xml.attributes():
print(f" 属性: {attr.name()} = {attr.value()}")
elif xml.isEndElement():
print(f"结束元素: {xml.name()}")
elif xml.isCharacters() and not xml.isWhitespace():
print(f"文本内容: {xml.text().strip()}")
if xml.hasError():
print(f"XML 解析错误: {xml.errorString()}")
file.close()
if __name__ == "__main__":
# 替换为你的 XML 文件路径
xml_file_path = r"D:\shawei\temp\jpbalance\temp\TaskTemplates\Shock Pulse.xml"
parse_xml_with_stream(xml_file_path)
import xml.etree.ElementTree as ET
def parse_xml_with_et(file_path):
try:
tree = ET.parse(file_path)
root = tree.getroot()
# 命名空间处理
ns = {'cm': 'http://www.pruftechnik.com/cm'}
print("=== 任务模板 ===")
print(f"版本: {root.get('version')}")
print(f"任务ID: {root.get('task_id')}")
print(f"名称: {root.get('name')}")
print(f"来源: {root.get('source')}")
meas_setup = root.find('cm:meas_setup', ns)
print("\n=== 测量设置 ===")
print(f"类型: {meas_setup.get('kind')}")
spectrum = meas_setup.find('cm:spectrum', ns)
print("\n=== 频谱配置 ===")
print(f"名称: {spectrum.get('name')}")
print(f"资源ID: {spectrum.get('res_id')}")
print(f"量值: {spectrum.get('quant')}")
print(f"线数: {spectrum.get('lines')}")
print(f"窗函数: {spectrum.get('window')}")
time_waveform = spectrum.find('cm:time_waveform_config', ns)
print("\n=== 时域波形配置 ===")
print(f"名称: {time_waveform.get('name')}")
print(f"资源ID: {time_waveform.get('res_id')}")
print(f"量值: {time_waveform.get('quant')}")
average = spectrum.find('cm:average', ns)
print("\n=== 平均设置 ===")
print(f"平均次数: {average.get('num')}")
print(f"重叠率: {average.get('overlap')}%")
print(f"因子: {average.get('factor')}")
print(f"类型: {average.get('type')}")
freq_spectrum = spectrum.find('cm:freq_spectrum', ns)
print("\n=== 频谱范围 ===")
print(f"最大频率: {freq_spectrum.get('max')}Hz")
print(f"最大类型: {freq_spectrum.get('max_type')}")
print(f"最小频率: {freq_spectrum.get('min')}Hz")
except ET.ParseError as e:
print(f"XML 解析错误: {e}")
except Exception as e:
print(f"发生错误: {e}")
if __name__ == "__main__":
xml_file_path = r"D:\shawei\temp\jpbalance\temp\TaskTemplates\Shock Pulse.xml"
parse_xml_with_et(xml_file_path)
<?xml version="1.0" encoding="UTF-8"?>
<task_template xmlns="http://www.xxxx.com/cm" version="4.0.0" task_id="00000000-0000-0000-0000-000000000003" name="Spec (a) 10Hz - 12800Hz" source="factory">
<meas_setup kind="trending_spectrum_amplitude">
<spectrum name="Spectrum Velocity" res_id="00000000-0000-0000-0000-000000000002" quant="acc" lines="51200" window="hanning">
<time_waveform_config name="Time Waveform Velocity" res_id="00000000-0000-0000-0000-000000000001" quant="acc"/>
<average num="3" overlap="60" factor="0.5" type="lin"/>
<freq_spectrum max="12800" max_type="time_based" min="10"/>
</spectrum>
</meas_setup>
</task_template>
#include <QCoreApplication>
#include <QFile>
#include <QXmlStreamReader>
#include <QDebug>
void parseTaskTemplateXml(const QString &filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "无法打开文件:" << filePath;
return;
}
QXmlStreamReader xml(&file);
while (!xml.atEnd() && !xml.hasError()) {
xml.readNext();
if (xml.isStartElement()) {
QString elementName = xml.name().toString();
if (elementName == "task_template") {
qDebug() << "=== 任务模板 ===";
qDebug() << "命名空间:" << xml.namespaceUri();
qDebug() << "版本:" << xml.attributes().value("version").toString();
qDebug() << "任务ID:" << xml.attributes().value("task_id").toString();
qDebug() << "名称:" << xml.attributes().value("name").toString();
qDebug() << "来源:" << xml.attributes().value("source").toString();
}
else if (elementName == "meas_setup") {
qDebug() << "\n=== 测量设置 ===";
qDebug() << "类型:" << xml.attributes().value("kind").toString();
}
else if (elementName == "spectrum") {
qDebug() << "\n=== 频谱配置 ===";
qDebug() << "名称:" << xml.attributes().value("name").toString();
qDebug() << "资源ID:" << xml.attributes().value("res_id").toString();
qDebug() << "量值:" << xml.attributes().value("quant").toString();
qDebug() << "线数:" << xml.attributes().value("lines").toString();
qDebug() << "窗函数:" << xml.attributes().value("window").toString();
}
else if (elementName == "time_waveform_config") {
qDebug() << "\n=== 时域波形配置 ===";
qDebug() << "名称:" << xml.attributes().value("name").toString();
qDebug() << "资源ID:" << xml.attributes().value("res_id").toString();
qDebug() << "量值:" << xml.attributes().value("quant").toString();
}
else if (elementName == "average") {
qDebug() << "\n=== 平均设置 ===";
qDebug() << "平均次数:" << xml.attributes().value("num").toString();
qDebug() << "重叠率:" << xml.attributes().value("overlap").toString() << "%";
qDebug() << "因子:" << xml.attributes().value("factor").toString();
qDebug() << "类型:" << xml.attributes().value("type").toString();
}
else if (elementName == "freq_spectrum") {
qDebug() << "\n=== 频谱范围 ===";
qDebug() << "最大频率:" << xml.attributes().value("max").toString() << "Hz";
qDebug() << "最大类型:" << xml.attributes().value("max_type").toString();
qDebug() << "最小频率:" << xml.attributes().value("min").toString() << "Hz";
}
}
}
if (xml.hasError()) {
qDebug() << "XML 解析错误:" << xml.errorString();
}
file.close();
}
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
parseTaskTemplateXml("task_template.xml");
return a.exec();
}