PDA

View Full Version : QXmlQuery query results



JovianGhost
1st September 2010, 00:58
I'm having a really annoying time with this.

I figured I'd use XML queries rather than manually traversing with XML DOM, because it would make my life easier. Well, so far that's not the case.

I have a sample XML file, as follows:



<root>
<mynode id="1">
<foo>this is foo.</foo>
<bar>this is bar.</bar>
</mynode>
<mynode id="2">
<foo>this is foo.</foo>
<bar>this is bar.</bar>
</mynode>
<mynode id="3">
<foo>this is foo.</foo>
<bar>this is bar.</bar>
</mynode>
<mynode id="4">
<foo>this is foo.</foo>
<bar>this is bar.</bar>
</mynode>
</root>

I want to find all nodes called "mynode".
At first I looked into evaluateTo(QXmlResultItems * result) but this doesn't let me access any properties of the nodes themselves, because QXmlItemis a very simplistic class.

Next I looked at evaluateTo(QStringList * target) but this returns the text of all descendants of "mynode" nodes as well, so I get all the "this is foo" and "this is bar". I don't want this.

What I want is the ability to match a bunch of nodes, and then access/manipulate each one individually. Is this possible?

[edit]
I found this example here: http://stackoverflow.com/questions/1286842/how-do-i-run-xpath-queries-in-qt

But when I run it, elementsByTagName only returns a single mynode, the first one. I don't understand what I'm doing wrong, since "res" contains the whole tree.

JovianGhost
1st September 2010, 03:02
Ok, I found the problem. The example in the link is incorrect.
When using setContent, the results need to be enclosed in another, arbitrary (root) tag. So it should read:



rhythmdb.setContent("<sometag>" + res + "</sometag>");


Now it works, but this seems like a really kludgy way of doing things-- executing the query, dumping the results into an XML buffer, than traversing the buffer.
I've always liked Qt because it's extremely powerful and easy to use at the same time. But this is just terrible. There MUST be an easier way than this.