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
Highlighted
New Developer
Posts: 26
Registered: ‎02-22-2013
My Device: BlackBerry Z10
My Carrier: Telus
Accepted Solution

Cant get XML Data save to work

I'm trying to store some data in an XML file.

 

I want to create and store this XML file, I copied this example straight from the documentation (http://developer.blackberry.com/cascades/documentation/device_platform/data_access/working_with_xml.... but it doesn't work:

// Create QVariantMap objects to contain the data for each employee
		QVariantMap firstEmployee;

		// Populate each object with data
		firstEmployee["firstName"] = QVariant("Mike");
		firstEmployee["lastName"] = QVariant("Chepesky");
		firstEmployee["number"] = QVariant("51649875");

		// Add each employee to a QVariantList
		QVariantList myEmployeeList = QVariantList() << QVariant(firstEmployee);
		                   
		// Add the employee list to a top-level QVariantMap, then wrap it in a QVariant
		QVariantMap topLevelEmployeeMap;
		topLevelEmployeeMap[".root"] = QVariant("employees");
		topLevelEmployeeMap["employee"] = QVariant(myEmployeeList);
		QVariant myData = QVariant(topLevelEmployeeMap);

		// Retrieve the working directory, and create a temporary .xml file in that
		// location
		QDir home = QDir::home();
		QTemporaryFile file2(home.absoluteFilePath("myXMLFile.xml"));
		qDebug() << "Came here";
		qDebug() << home.absoluteFilePath("myXMLFile.xml");
		// Open the file that was created
		if (file2.open()) {
		    // Create an XmlDataAccess object and save the data to the file
			qDebug() << "I came in this if statement";
		    XmlDataAccess xda;
		    xda.save(myData, &file2);
		}
		else
			qDebug() << "I didn't come in this if statement";

 

After trying everything, I can't see the .xml file to show in file explorer. The print statement which prints location .../data/ and the second print statement shows that it goes in the if statement.

 

I can't even get a basic XML file to save, so I'm wondering if I'm doing something wrong.

 

Thanks

Developer
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: Cant get XML Data save to work

[ Edited ]

Hey there,

 

Well, the thing about it is that it's using a QTemporaryFile object (file2) and according to the description of the QTemporaryFile class:

 

"....QTemporaryFile is used to create unique temporary files safely. The file itself is created by calling open(). The name of the temporary file is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing file), and the file will subsequently be removed upon destruction of the QTemporaryFile object....."

 

Which would be your problem if the file2 object is destroyed in your app....  Are you checking for the file while the app is running?  If not, you should use QFile instead so that the file will still be there upon completion of your app... etc...etc

 

https://developer.blackberry.com/cascades/reference/qfile.html

New Developer
Posts: 26
Registered: ‎02-22-2013
My Device: BlackBerry Z10
My Carrier: Telus

Re: Cant get XML Data save to work

This solved my problem! Thank you very much.

New Developer
Posts: 2
Registered: ‎02-02-2013
My Device: BlackBerry Z10
My Carrier: EE

Re: Cant get XML Data save to work

I am trying to do this aswell. Could you share how you done it? Thanks

New Developer
Posts: 1
Registered: ‎01-06-2015
My Device: Q10
My Carrier: Mobifone

Re: Cant get XML Data save to work

Hi there,

I'm stucking with this example too. Would you mind helping me to show more detail how you solve this ?

Thank you very much!

Best regards.