Jav*_*ast 1 c++ xml qt xml-parsing
我需要将 c++(!11)/QT 中的 XML 文件解析为包含每个值及其 xml 标记父结构的向量。
我是 QT 的新手,我知道他们的库中有一些不错的选择。然而,我发现的大部分内容都集中在那些提前知道标签名称的人身上。对我来说,我需要更通用的东西。标签名称(和值)与我的目的无关,可以是任何东西,我的重点是保存每个值的标签结构。为此采取的最佳方法是什么?QDomDocument?
*注意:实际的 xmls 在树结构长度上会复杂得多。
示例输入
测试文件
<MainTag>
<description>Test Description</description>
<type>3</type>
<source>
<description>Source test Description1</description>
<type>4</type>
</source>
<source>
<description>Source test Description2</description>
<type>5</type>
<name>
<element>1</element>
</name>
</source>
</MainTag>
Run Code Online (Sandbox Code Playgroud)
示例输出
(包含在 C++ 向量中的字符串行):
description=Test Description
type=3
source.description=Source test Description1
source.type=4
source.description=Source test Description2
source.type=5
source.name.element=1
Run Code Online (Sandbox Code Playgroud)
在解析 XML 文件时,我发现导航 XML 的 DOM 比使用流解析器更灵活,因为您的代码不太了解元素的顺序,而更多地关注结构和内容。
对于 DOM 导航,您可以使用QDomDocument和相关类:
首先使用 XML 文件的内容初始化一个文档 QDomDocument::setContent
提取文档元素(XML 的父元素)
其余的包括使用QDomElement::firstChildElement(const QString& tagName)和QDomElement::nextSiblingElement(const QString& tagName)(QDomElement继承自QDomNode)导航结构;由于您不知道标签名称,只需将其保留为空即可返回该级别的所有元素
要提取您可以使用的标签内容,QDomNode::nodeValue这里的技巧是提取第一个不是元素的节点的值(请参阅下面的代码)
虽然您的问题中没有提到,但如果您需要访问您拥有的属性 QDomElement::attribute
解析未知 XML 的示例代码
此代码解析 XML 并提取标记名称及其文本。它不提取属性或空节点。
注意:我已将给定示例中的结束标记更正<MainTag>为</MainTag>.
#include <QtXml>
#include <QtCore>
#include <vector>
#include <iostream>
// Recursive function to parse the XML
void parseXML(const QDomElement& root, const QString& baseName, std::vector<QString>& v)
{
// Extract node value, if any
if (!baseName.isEmpty() && !root.firstChild().nodeValue().isEmpty()) { // the first child is the node text
v.push_back(baseName + "=" + root.firstChild().nodeValue());
}
// Parse children elements
for (auto element = root.firstChildElement(); !element.isNull(); element = element.nextSiblingElement()) {
parseXML(element, baseName + "." + element.tagName(), v);
}
}
int main(int argc, char* argv[])
{
const QString content = "<MainTag>"
"<description>Test Description</description>"
"<type>3</type>"
"<source>"
" <description>Source test Description1</description>"
" <type>4</type>"
"</source>"
"<source>"
" <description>Source test Description2</description>"
" <type>5</type>"
" <name>"
" <element>1</element>"
" </name>"
"</source>"
"</MainTag>";
std::vector<QString> v;
QDomDocument xml("xml");
xml.setContent(content);
parseXML(xml.documentElement(), "", v); // root has no base name, as indicated in expected output
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << it->toStdString() << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
来自文件的 DOM
要从文件中填充 DOM,请使用setContent以下代码更改该行(为简单起见,省略了错误检查):
QFile file(filePath);
file.open(QFile::ReadOnly);
xml.setContent(file.readAll());
Run Code Online (Sandbox Code Playgroud)