使用 Qt 搜索具有特殊父级的 XML 属性
Searching for XML attributes with special parent in with Qt
如何管理我的XML读取器,搜索具有特殊父元素的属性?
例如,我的XML文件:
<section name="aPoly">
<attribute key="id" type="int">1</attribute>
<attribute key="label" type="String">2</attribute>
<section name="graphic">
<attribute key="x" type="double">780.0</attribute>
<attribute key="y" type="double">240.0</attribute>
<attribute key="w" type="double">78.0</attribute>
<attribute key="h" type="double">78.0</attribute>
</section>
</section>
...
...
<section name="line">
<attribute key="source" type="int">0</attribute>
<attribute key="target" type="int">4</attribute>
<section name="graphic">
<attribute key="fill" type="String">#000000</attribute>
<section name="poly">
<section name="pt">
<attribute key="x" type="double">377.0</attribute>
<attribute key="y" type="double">240.0</attribute>
</section>
<section name="pt">
<attribute key="x" type="double">570.0</attribute>
<attribute key="y" type="double">240.0</attribute>
</section>
<section name="pt">
<attribute key="x" type="double">570.0</attribute>
<attribute key="y" type="double">330.0</attribute>
</section>
</section>
</section>
</section>
有多个属性具有key = "x"
.我想将第 name="aPoly"
节的 x 值保存在一个数组中,将第 name="line"
节的 x 值保存到另一个数组中。
我正在搜索属性并将其保存到 QVector。(搜索所有 X 属性)。
QDomElement root = document.documentElement();
QDomNodeList key = root.elementsByTagName("attribute");
for(int i = 0; i < key.count(); i++)
{
QDomNode keynode = key.at(i);
if(keynode.isElement())
{
QDomElement att = keynode.toElement();
if (att.attribute("key") == "x"){
xarray.push_back(att.text());
}else if (att.attribute("key") =="y") {
yarray.push_back(att.text());
}...
我可以更改哪些内容来实现此目的?
编辑:
为什么他不填满我的阵列?它总是空的?我的错误在哪里?
QXmlQuery query;
if (query.setFocus(&file))
{
query.setQuery(QString("section/section/section[@name='aPoly']/section/attribute[@key='x']"));
if (query.isValid())
{
QString result;
query.evaluateTo(&result);
QDomDocument dom;
dom.setContent(QString("<Data>%1</Data>").arg(result));
QDomNodeList children = dom.documentElement().childNodes();
for (int i = 0; i < children.count(); i++)
{
QDomNode nod = children.at(i);
if(nod.isElement())
{
QDomElement art = nod.toElement();
Newarray.push_back(art.text());
}
}
}
}
qDebug() << "rnNewArray : " <<QList<QString>::fromVector(Newarray);
您可以使用
XQuery
。类似于以下内容的 XPath 可用于您的目的,前提是section
元素位于 sections
元素内,并且它是 xml 文档的根目录。
"sections/section[@name='aPoly']/attribute[@key='x']"
例如。
QXmlQuery query;
if (query.setFocus(yourXmlString)
{
query.setQuery("sections/section[@name='aPoly']/attribute[@key='x']");
if (query.isValid())
{
QString result;
query.evaluateTo(&result);
QDomDocument dom;
dom.setContent(QString("<Data>%1</Data>").arg(result));
QDomNodeList children = dom.documentElement().childNodes();
for (int i = 0; i < children.count(); ++i)
{
//children.at(i) is an attribute element which matches your criteria.
}
}
}
相关文章:
- 使用 RapidXML 更新C++中的 XML 节点和属性值
- 按属性搜索和提取 XML 节点
- 尝试使用 RapidXML 将唯一的 XML 属性追加到节点
- 将具有 xml 属性的子树添加到提升属性树
- 如何使用 pugixml 正确访问 xml 属性?
- 使用提升属性树检查 xml 文件中是否存在元素
- 如何在Qt中解析未知的xml并获取其中的所有属性
- 如何将Boost属性树的子树序列化为XML
- 提升属性树:如何使用xml文件获取子树的子级
- 如何手动创建具有不同 XML 属性的提升 ptree
- 提升属性树(XML)删除空白行
- 使用QT的C 处理DOM处理后的XML属性顺序
- 使用 Boost 属性树解析 SVG (XML)
- 使用 Qt 搜索具有特殊父级的 XML 属性
- 正在将具有多个值的XML属性加载到不同的表单元格中
- 使用 xerces 读取 xml 属性的值
- 如何使用tinyxml将xml属性转换为c++类
- 如何获得xml属性与此语法使用Qt DOM
- 如何手动创建带有XML属性的boost ptree
- 使用Boost解析XML属性