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
Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Parse xml with Qt?

I am trying to parse some xml (a webservice response) using Qt.

This is the xml structure:

<Attributes>
   <item>
       <Objectname>o</Objectname>
       <Attributename>a</Attributename>
       <Value>v</Value>
    </item>
   //more items
</Attributes>
<Version>1</Version>

 and this is my (not working) code:

QtSoapStruct &soapStruct = (QtSoapStruct &) resp.method();
QtSoapArray &attributes = (QtSoapArray &) soapStruct["Attributes"];
QtSoapArray &items = (QtSoapArray &) attributes[0];
for (int i = 0; i < items.count(); ++i) {
   QtSoapStruct &item = (QtSoapStruct &) items[i];
   qDebug() << i;
   qDebug() << item["Objectname"].toString();
   qDebug() << item["Attributename"].toString();
   qDebug() << item["Value"].toString();
}

 Can somebody give me a hint what i am doing wrong?

 

 

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Super Contributor
Posts: 306
Registered: ‎08-10-2010
My Device: Not Specified

Re: Parse xml with Qt?

Hi simon_hain,

 

Here is the link of the application in which parse the xml with cascade is given from there you can get some idea to resolve your issue.

 

URL:

 https://developer.blackberry.com/cascades/files/webinars/dealing_with_data.pdf [Webinar Link]

API link:  https://developer.blackberry.com/cascades/documentation/device_platform/networking/tutorial_http_com...

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Parse xml with Qt?

The issue could be in your casts.

You are doing reinterpret_cast, which is inherently dangerous (unless you were using that syntax to show logic only as a sketch).   e.g. are you absolutely sure that resp.method returns an object with a lifetime longer than your routine that is a QtSoapStruct?   Are you absolutely sure that soapStruct has a value for the key "Attributes" and that the value is a QtSoapArray&?  (What QtSoapType does it return if it doesn't have that key but instead adds a value?)

 

It's wise to avoid (MyType&) casts and wise to avoid reinterpret_cast, and where you use them, these should be wrapped in defensive code.

 

What happens when you step through this code in the debugger?

 

Stuart

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Parse xml with Qt?

Hello Stuart, thanks for chiming in!

My expert status was solely earned with java, i am in no way an experienced c++ developer, so my code is mostly done in trial&error as i seem unable to find samples that cover more than the simplest part.

The output of the code is just empty strings with the iterator.

I had it working some time ago, but after my holidays and updating the DevAlpha the code did no longer work as expected.

The code is (in part) based on Nokias population sample, i got some sample code from RIM as well, but the xml there is much simpler.

How would you parse the xml? Feel free to point me to any library (as long as it has a permissive license).

@Meghasanghvi: Thanks for the link, unfortunately the document states only "pass to c++ for parsing", which is exactly what i am trying to do.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Highlighted
Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Parse xml with Qt?

[ Edited ]

Update:
I did it!

solution:
don't use resp.returnValue(), instead use resp.method().

 

I checked with http://www.xml.com/pub/a/2000/02/09/feature/index.html and the response method is something completly different, so either i still don't understand how QtSoap works or the implementation is buggy.

Now don't ask me why, the Nokia sample uses returnValue, but it seems that it returns only the first element of the response, in this case the "Attributes" array/element.
(i know similar cases from java development when the type mapping is incomplete on ksoap2)

Next step was to remove the index0-access: QtSoapArray &items = (QtSoapArray &) attributes[0];
Instead i can directly iterate on attributes.

Here is (part of) the running code:

QtSoapStruct &soapStruct = (QtSoapStruct &) resp.method();
QtSoapArray &attributes = (QtSoapArray &) soapStruct["Attributes"];
for (int i = 0; i < attributes.count(); ++i) {
        qQtSoapStruct &item = (QtSoapStruct &) attributes[i];
        qDebug() << "Item " + i;
        qqDebug() << item["Objectname"].toString();
        qqDebug() << item["Attributename"].toString();
        qqDebug() << item["Value"].toString();
}
QtSoapArray &boxes = (QtSoapArray &) soapStruct["Box"];
...

My next step would be to save the values in an SQL database, but as you (Stuart) mentioned unsafe casts, could you tell me how to improve that part? It would be best if the code would still run! Smiley Wink

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter