c++ - QXMLStreamReader 无法读取属性
问题描述
我想解析以下xml(它是一个xsd):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name = "elementName">
<xs:simpleType>
<xs:restriction base = "xs:string">
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
在某些时候,我调用以下方法,该方法应读取的base
属性restriction
:
void String::getFromBase() const {
QXMLStreamReader m_node; // in true code it's a class member already initialized
m_node.readNextStartElement();
auto name = stripNamespace(m_node.name().toString()); // convert xs:restriction to restriction
if (name != "restriction") {
throw Exception("Restriction is not found when reading string data");
}
bool hasBase{false};
auto x = m_node.attributes().size(); // x is zero but it should be one
for (const auto& it : m_node.attributes()) {
auto attrName = stripNamespace(it.name().toString());
if (attrName == "base") {
hasBase = true;
break;
}
}
if (!hasBase) {
throw Exception("Restriction needs 'base' attribute");
}
auto attribute = stripNamespace(m_node.attributes().value(BaseLabel).toString());
if (attribute == "string") {
int i = 0;
}
}
该方法检查QXMLStreamReader
is中的实际元素restriction
,并且该检查通过,但随后 的大小attributes()
为零,因此阅读器没有找到任何属性。因为我restriction
在测试 XSD 中只有一个,所以属性应该在那里。如何base
正确读取属性值?
解决方案
推荐阅读
- c# - 数据库上的列名无效
- python - 如何在 python(pandas) 中保存数据
- python - ffmpeg 作为子进程需要太长时间
- c++ - N皇后问题。从函数c ++返回数组
- google-chrome-extension - 如何将数据从本机应用程序(AdiIRC,IRC 客户端)传递到我的 Chrome 扩展程序?
- bash - 使用 bash 转换字符串
- python - FPS 如何影响视频处理
- python-3.x - 如何与未关闭/完成的子进程交互?
- android - 如何从一个以方向为坐标的反应原生应用程序午餐谷歌地图应用程序
- ruby-on-rails - 使用 Ruby on Rails 进行管理面板系统设计