如何在QT中运行XPath查询? [英] How do I run XPath queries in QT?

查看:255
本文介绍了如何在QT中运行XPath查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在QT中运行XPath查询?

How do I run an XPath query in QT?

我需要在某个属性中对特定标签进行排序。 QXmlQuery文档是可读的。

I need to sort out certain tags with specific values in a certain attribute. The QXmlQuery documentation is anything but legible.

我正在解析的模式是Rhythmbox DB格式:

The schema I'm parsing is the Rhythmbox DB format:



<rhythmdb version="1.6">
  <entry type="ignore">
    <title></title>
    <genre></genre>
    <artist></artist>
    <album></album>
    <location>file:///mnt/disk/music/Cover.jpg</location>
    <mountpoint>file:///mnt/disk</mountpoint>
    <mtime>1222396828</mtime>
    <date>0</date>
    <mimetype>application/octet-stream</mimetype>
    <mb-trackid></mb-trackid>
    <mb-artistid></mb-artistid>
    <mb-albumid></mb-albumid>
    <mb-albumartistid></mb-albumartistid>
    <mb-artistsortname></mb-artistsortname>
  </entry>
  <entry type="song">
    <title>Bar</title>
    <genre>Foobared Music</genre>
    <artist>Foo</artist>
    <album>The Great big Bar</album>
    <track-number>1</track-number>
    <disc-number>1</disc-number>
    <duration>208</duration>
    <file-size>8694159</file-size>
    <location>file:///media/disk/music/01-Foo_-_Bar.ogg
    <mountpoint>file:///media/disk
    <mtime>1216995840</mtime>
    <first-seen>1250478814</first-seen>
    <last-seen>1250478814</last-seen>
    <bitrate>301</bitrate>
    <date>732677</date>
    <mimetype>application/x-id3</mimetype>
    <mb-trackid></mb-trackid>
    <mb-artistid></mb-artistid>
    <mb-albumid></mb-albumid>
    <mb-albumartistid></mb-albumartistid>
    <mb-artistsortname></mb-artistsortname>
  </entry>
</rhythmdb>

这是你的基本XML模式,它有一组结构化条目。我的意图是过滤掉类型为ignore的条目。

This is your basic XML Schema which has a collection of structured entries. My intention was to filter out the entries with the type 'ignore'.

推荐答案

相关文档位于: http://qt-project.org/doc/qt-4.8/qxmlquery .html#running-xpath-expressions

我的解决方案是使用QXmlQuery生成一个XML文件,然后使用QDomDocument再次解析。 / p>

The solution I came to was to use QXmlQuery to generate an XML file then parse it again using QDomDocument.



RhythmboxTrackModel::RhythmboxTrackModel()
{
    QXmlQuery query;
    QXmlQuery entries;
    QString res;
    QDomDocument rhythmdb;


    /*
     * Try and open the Rhythmbox DB. An API call which tells us where
     * the file is would be nice.
     */
    QFile db(QDir::homePath() + "/.gnome2/rhythmbox/rhythmdb.xml");
    if ( ! db.exists()) {
        db.setFileName(QDir::homePath() + "/.local/share/rhythmbox/rhythmdb.xml");
        if ( ! db.exists())
            return;
    }

    if (!db.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    /*
     * Use QXmlQuery to execute and XPath query. Check the version to
     * make sure.
     */
    query.setFocus(&db);
    query.setQuery("rhythmdb[@version='1.6']/entry[@type='song']");
    if ( ! query.isValid())
        return;

    query.evaluateTo(&res);
    db.close();


    /*
     * Parse the result as an XML file. These shennanigans actually
     * reduce the load time from a minute to a matter of seconds.
     */
    rhythmdb.setContent("" + res + "");
    m_entryNodes = rhythmdb.elementsByTagName("entry");


    for (int i = 0; i < m_entryNodes.count(); i++) {
        QDomNode n = m_entryNodes.at(i);
        QString location = n.firstChildElement("location").text();

        m_mTracksByLocation[location] = n;
    }

    qDebug() << rhythmdb.doctype().name();
    qDebug() << "RhythmboxTrackModel: m_entryNodes size is" << m_entryNodes.size();
}

如果有人想知道,这是我的代码 Mixxx项目的最近一个分支,特别是features_looping分支。

In case anyone is wondering, this is my code taken from a recent branch of the Mixxx project, specifically the features_looping branch.

我不喜欢这个解决方案的东西是:

The things I dislike about this solution are:


  • 解析XML两次

  • 连接

这篇关于如何在QT中运行XPath查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆