07-20-2011 05:15 AM - edited 07-20-2011 05:22 AM
UPDATE: Just found out that documentToBlob is only supported in OS5. How do I do the same in OS6?
http://www.blackberry.com/developers/docs/webworks
I wrote an app several months ago which has been working fine with OS5. But it does not work with OS6. Here's a simple example of creating a file, which the app needs to do:
config.xml
<?xml version="1.0" encoding="utf-8" ?>
<widget xmlns="http://www.w3.org/ns/widgets"
xmlns:rim="http://www.blackberry.com/ns/widgets"
rim:header="RIM-Widget:rim/widget"
version="1.0.0">
<name>File Test</name>
<description>Testing file</description>
<author href="http://www.example.com/" rim:copyright="no copyright" email="info@example.com">Example</author>
<license href="" />
<feature id="blackberry.utils" version="1.0.0"/>
<feature id="blackberry.io.dir" version="1.0.0"/>
<feature id="blackberry.io.file" version="1.0.0"/>
<feature id="blackberry.ui.menu" version="1.0.0"/>
<feature id="blackberry.system.event" version="1.0.0"/>
<feature id="blackberry.invoke" version="1.0.0"/>
<feature id="blackberry.invoke.MapsArguments" version="1.0.0"/>
<content src="test.html" />
</widget>
test.html
<html>
<head>
<title>Test</title>
<meta name="viewport" id="viewport" content="initial-scale=1.0,user-scalable=no" />
<script src="scripts/test.js" type="text/javascript"></script>
</head>
<body onload="loadTest();">
testing
</body>
</html>
scripts/test.js
function loadTest(){
alert("start");
var filePath = "file:///store/home/user/appTest.xml";
var xmlString = "<test>test_data</test>";
var parser = new DOMParser();
var doc = parser.parseFromString(xmlString, "text/xml");
try{
var blobData = blackberry.utils.documentToBlob(doc);
blackberry.io.file.saveFile(filePath, blobData);
alert("file saved");
} catch(e) {
alert("unable to save file: "+e.message);
}
if(blackberry.io.file.exists(filePath))
alert("file exists");
else
alert("file cannot be found");
alert("end");
}
OS6 simulator (6.0.0.337) output:
Any idea why it doesn't work?
Solved! Go to Solution.
07-21-2011 11:15 AM
Can you just use stringToBlob?
http://www.blackberry.com/developers/docs/webworks
button below the post(s)07-21-2011 12:11 PM
documentToBlob has been deprecated in 6.0, it's only available in 5.0.
07-21-2011 12:52 PM
Since the document is a JavaScript object you should be able to do a toString() on it.
So you could do something like the following pseudo code
var foo = blackberry.utils.stringToBlob(document.toString());
10-26-2011 10:08 AM
Thanks for the reply. I can now save a file, but it's empty in both OS5 & OS6. Here's my updated .js file with your suggestions (the config.xml, etc is same as my first post):
function loadTest(){
alert("start");
var filePath = "file:///store/home/user/appTest.xml";
var xmlString = "<test>test_data</test>";
var parser = new DOMParser();
var doc = parser.parseFromString(xmlString, "text/xml");
var blobData = blackberry.utils.stringToBlob(doc.toString());
try{
blackberry.io.file.saveFile(filePath, blobData);
alert("file saved");
} catch(e) {
alert("unable to save file: "+e.message);
}
if(blackberry.io.file.exists(filePath))
alert("file exists");
else
alert("file cannot be found");
alert("end");
}
Has anyone got a working copy of saving an XML document on OS6 or OS7?!
Thanks for your help.
10-26-2011 11:44 AM
Hi confinedspace,
That code doesn't seem to give a reasonable string in my browser. It seems like if it parses properly there should be an "xml" property that holds the string.
button below the post(s)10-26-2011 12:32 PM
Thanks jeffheifetz.
I'm following what has been recommended by the BB documentation: https://bdsc.webapps.blackberry.com/html5/apis/bla
The documentation to save a file doesn't work either as it uses a depriciated function (documentToBlob): https://bdsc.webapps.blackberry.com/html5/apis/bla
Is there an example anywhere which shows how to save an XML document on OS6?
11-15-2011 11:16 AM
I couldn't find a solution to this, nor could anyone help. I had to rewrite my application to store data in a database instead of an XML file.