Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
joanpuigsanz
Posts: 27
Registered: ‎07-30-2011
My Device: Z10

How to iterate XmlDataModel

Hi there!

 

There is a way to iterate the XmlDataModel using C++???

 

Thanks!

Contributor
craigmj
Posts: 13
Registered: ‎10-01-2011
My Device: 9900

Re: How to iterate XmlDataModel

Use XmlDataModel::childCount(QVariantList) to obtain the child count for a particular indexPath.

 

Every indexPath is a QVariantList of int's, each int being an index. The empty indexPath will be the path to the root element.

 

So:

 

QVariantList path;

qDebug() << xmlDataModel.childCount(path);

 

will output the number of child elements.

 

You can retrieve the elements with:

 

XmlDataModel::data(QVariantList)

 

Contributor
joanpuigsanz
Posts: 27
Registered: ‎07-30-2011
My Device: Z10

Re: How to iterate XmlDataModel

Which QVariantList  parameter should I need to set? If I wanna see all the elements for instance? it is not very clear for me how it should be according to the documentation:

 

https://developer.blackberry.com/cascades/reference/bb__cascades__xmldatamodel.html#data

Contributor
craigmj
Posts: 13
Registered: ‎10-01-2011
My Device: 9900

Re: How to iterate XmlDataModel

Well, it's a tree structure (an XML document), so if your indexPath has no elements:

 

[]

 

then childCount will return the number of root nodes in your document (which it turns out is the number of elements beneath the root node):

 

<root>

<item>One</item>

<item>Two</item>

<item>Three<subitem>A</subitem></item>

</root>

 

So :

 

QVariantList path;

// path is empty []

m_model->childCount(path);  // returns 3 - 3 items

path.append(QVariant(2));

m_model->data(path);   // returns QVariantMap for item Three

 

I've not actually played with going deeper, but in theory:

 

path.append(0);

m_model->data(path);  // returns QVariantMap for subitem A

 

In the last case, path now has [2,0] as the indexPath.

 

Easiest is to play around with this and use qDebug() to see what it does.

Contributor
joanpuigsanz
Posts: 27
Registered: ‎07-30-2011
My Device: Z10

Re: How to iterate XmlDataModel

It works :smileyhappy:

 

But I can not change the data.... do you know how to change the data in the model?? for instance, the item 

 

<item>One</item>

 

I wnat to change it for :

 

<item> this is the one </item>