tags:

views:

1489

answers:

2

How do I run an XPath query in QT?

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

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>

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'.

+2  A: 

Do you mean Qt as in the Qt toolkit? If so, Patternist might be of use.

Frerich Raabe
Yes, I do mean the QT Toolkit. I have since implemented XPath using QXmlQuery. Feel free to fill in your answer. Hopefully, it's more elegant than my solution.
Phillip Whelan
+4  A: 
Phillip Whelan
You don't need to parse the XML twice; simply use a different QXmlQuery::evaluateTo() overload. Simply use 'QXmlResultItems result; query.evaluateTo( ' and then iterate over 'result' to get all matching nodes. See http://doc.trolltech.com/main-snapshot/qxmlresultitems.html
Frerich Raabe
XMLResultItems, XMLItems or XMLNodeItems are not adequate as far as I could tell from the documentation. As the documentation says: "Because QXmlNodeModelIndex is intentionally a simple class, it doesn't have member functions for accessing the properties of nodes."
Phillip Whelan