开发者

Converting QXmlItem to QtDomElement or similar?

开发者 https://www.devze.com 2022-12-22 13:06 出处:网络
I\'m parsing a fairly complicated XML file of the following structure: <root>......<item><subitem id="1"/><text>text1</text></item><item><subit

I'm parsing a fairly complicated XML file of the following structure:

<root>

...

...

<item>

<subitem id="1"/>

<text>

text1

</text>

</item>

<item>

<subitem id="2"/>

<text>

text2

</text>

</item>

...

开发者_JAVA百科 <item>

...

</item>

...

</root>

It's pretty crude but you get my drift I hope. I'm primarily interested in "item" nodes. So I wrote the following code (directly out of the Qt's online manual):

QXmlQuery query;
query.setQuery("//item/");

QXmlResultItems result;
query.evaluateTo(&result);

QXmlItem item(result.next());
while (!item.isNull()) 
{
  if (item.isNode())
  {
      // WHAT DO I DO NOW?
  }
  item = result.next();
}

Now, QXmlItem appears to represent two concepts, a literal value (like a string) or a Node, (which is what item.isNode() is doing). Unfortunately, I can't grasp how to convert the QXmlItem to something that will query-able again. In particular from the example above I'd like to grab the "id" attribute, and the text element. Can I do this using the XQuery approach, or am I way off base here?

Any advice?

Thanks!


You can use QXmlItem to modify the focus of your query. For example:

QXmlItem item(result.next());
while (!item.isNull()) 
{
  if (item.isNode())
  {
    query.setFocus(item);
    query.setQuery("./text/string()");
    QString text;
    query.evaluateTo(&text);
  }
  item = result.next();
}

will retrieve the <text> value of the <item>.


QXmlQuery is one crummy piece of Qt documentation, but i would say that you write your query to return the items that you actually want i.e. (this is an uneducated guess)

query.setQuery("//item/subitem | //item/text");

W3Schools has a Tutorial on XPath that might help

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号