c++ - 使用 Qt 搜索具有特殊父级的 XML 属性

标签 c++ xml qt qdomdocument

我如何管理我的 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() << "\r\nNewArray : " <<QList<QString>::fromVector(Newarray);

最佳答案

您可以为此使用 XQuery。假设 section 元素位于 sections 元素内并且它是 xml 文档的根,则可以使用类似于以下的 XPath。

"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.
        }
    }
}

关于c++ - 使用 Qt 搜索具有特殊父级的 XML 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29916399/

相关文章:

c++ - 不同的对象作为同一个函数的参数

java - 检测 TCP 数据包丢失

qt - QML: "x"属性到底有什么作用,与 ListView 中的 anchors.leftMargin/rightMargin 有什么区别?

java - Android:将文本设置为复选框不起作用

c++ - 动态添加覆盖小部件

c++ - 高速有效地更新 QTableView

c++ - 打印通过命令行传递的参数

c++ - 如何链接目录中的所有库?

xml - 在终端中编辑 XML 文件

xml - 如何使用 R 将 data.frame 转换为 xml 文件?