无法读取嵌套 xml 标记的属性 (QXmlStreamReader)
Can't read attributes of nested xml tag (QXmlStreamReader)
我有一个XML文件(我必须简化它):
<Line line1_attr1 = "value1" line1_attr2 = "value2">
<Term line1_term1_attr1 = "term1value1" line1_term1_attr2 = "term1value2">
term content
</Term>
<Term line1_term2_attr1 = "term2value1" line1_term2_attr2 = "term2value2">
term content
</Term>
</Line>
<Line line2_attr1 = "value1" line2_attr2 = "value2">
<Term line2_term1_attr1 = "term1value1" line2_term1_attr2 = "term1value2">
term content
</Term>
<Term line2_term2_attr1 = "term2value1" line2_term2_attr2 = "term2value2">
term content
</Term>
</Line>
属性存储在两个qmap中:mapString
(Line属性)和MapTerm
(Term属性)。我可以读取Line
标签的属性,但不能读取Term
标签的属性。无论是这个
if(token == QXmlStreamReader::StartElement)
{
if (xml.name() == "Line")
{
QXmlStreamAttributes attrib = xml.attributes();
for(auto e : mapString->keys())
{
mapString->insert(e, attrib.value(e).toString());
}
continue;
if (xml.name() == "Term")
{
QXmlStreamAttributes attrib = xml.attributes();
for(auto e : mapTerm->keys())
{
mapTerm->insert(e, attrib.value(e).toString());
}
continue;
}
}
和
if(token == QXmlStreamReader::StartElement)
{
if (xml.name() == "Line")
{
QXmlStreamAttributes attrib = xml.attributes();
for(auto e : mapString->keys())
{
mapString->insert(e, attrib.value(e).toString());
}
continue;
}
if (xml.name() == "Term")
{
QXmlStreamAttributes attrib = xml.attributes();
for(auto e : mapTerm->keys())
{
mapTerm->insert(e, attrib.value(e).toString());
}
continue;
}
如果正常工作,则if (xml.name() == "Term")内的代码不会执行。
这个循环更简洁,应该可以工作:
QXmlStreamReader xml;
...
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
QMap<QString, QString> * map = nullptr;
if (xml.name() == "Line") map = mapString;
else if (xml.name() == "Term") map = mapTerm;
else continue;
QXmlStreamAttributes attrib = xml.attributes();
for (auto e : map->keys())
map->insert(e, attrib.value(e).toString());
}
}
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- C++ 命名参数习惯用语 - 未设置字符串属性
- 使用 CTRP 时,是否访问访问父构造函数 UB 中的子属性?