load XML from assets/www... AJAX? FileSystem? FileReader?

1,198 views
Skip to first unread message

DieHardDigitalDeveloper

unread,
Oct 5, 2011, 6:02:37 AM10/5/11
to phon...@googlegroups.com
*sigh* it seems like it should be a simple thing.  I have an XML file that contains set information (name, description and elements in the set) and I want to load it into my app.

I'm using phonegap 1.1.0 with jquery mobile and jquery 1.6.2 through the Eclipse IDE.

I've tried $.ajax({
url: 'set.xml',
dataType: 'xml',
success: function() {
  alert('Yay!');
},
error: function() {
  alert('LLLLLOSER!')
}
})

comes up loser.

I tried getting there through the filesystem but there really isn't a decent, working example of how to do that. 

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, GotIt, fail);  won't hit either GotIt function or fail function and I can't figure out how to debug with Eclipse to find out what is actually happening.

I tried reader = new FileReader('sets.xml') with handlers for onloadend and onerror and that ignores me like the requestFileSystem call.

I know I'm rambling but I've been beating my head on this brick wall for six hours and I'm exhausted.  Is there a concise tutorial short of reading the 2000 line dissertation at w3c?

On a side note, I'm adapting this from an Adobe Air app and the XML file is already written so I'd prefer to use it but if someone sees a roadblock ahead, I can convert to JSON and eval the contents of sets.xml.

Here is my code atm.

   function onDeviceReady() {
        element = document.getElementById('deviceProperties');
        $(element).html('Document ready...<br>');
        reader = new FileReader('Bells.xml');
        $(element).append('getting file<br>');
        reader.onloadend = function(evt) {
        $(element).append("read success");
        $(element).append(evt.target.result);
         }
         reader.onerror = function(evt) {
             $(element).append("read failed");
         }
    }

Have also tried this...

   function onDeviceReady() {
        var element = document.getElementById('deviceProperties');
        $(element).html('Document ready...<br>');
        window.requestFileSystem(LocalFileSystem.PERSISTENT,0, onFileSystemSuccess, fail);
        $(element).append('getting file system...<br>');
    }
      function onFileSystemSuccess(fileSystem) {
            alert('got file system');
               fileSystem.root.getFile("Bells.xml", {create:false},
onResolveSuccess, fail);
        }
   
        function onResolveSuccess(fileEntry) {
            alert('got file');
        }
   
        function fail(evt) {
            alert('--'+evt.target.error.code);
        }

and the $.ajax code above.

Simon MacDonald

unread,
Oct 5, 2011, 11:17:47 AM10/5/11
to phon...@googlegroups.com
You can't use a FileReader as you xml file is not on the file system. It is part of your .apk so in Android parlance you'd need a FileDescriptor object in order to access it. This is how I would go about reading an .xml file from the assets directory without using jQuery:

        var request = new XMLHttpRequest();

        request.open("GET", "file:///android_asset/www/xml.xml");

        request.onreadystatechange = function() {//Call a function when the state changes.

            console.log("state = " + request.readyState);

            if(request.readyState == 4) {

                console.log("*"+request.responseXML+"*");

               }

        }

        console.log("asking for xml");

        request.send();


Simon Mac Donald
http://hi.im/simonmacdonald


--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com

bespired

unread,
Oct 5, 2011, 1:18:44 PM10/5/11
to phonegap


Webkit does not read from file://



On 5 okt, 12:02, DieHardDigitalDeveloper <mark.cicche...@gmail.com>
wrote:
> *sigh* it seems like it should be a simple thing.  I have an XML file that
> contains set information (name, description and elements in the set) and I
> want to load it into my app.
>
> I'm using phonegap 1.1.0 with jquery mobile and jquery 1.6.2 through the
> Eclipse IDE.
>
> I've tried $.ajax({
> url: 'set.xml',
> dataType: 'xml',
> success: function() {
>   alert('Yay!');},
>
> error: function() {
>   alert('LLLLLOSER!')
>
> }
> })
>
> comes up loser.
>
> I tried getting there through the filesystem but there really isn't a
> decent, working example of how to do that.  
>
> window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, GotIt, fail);  won't
> hit either GotIt function or fail function and I can't figure out how to
> debug with Eclipse to find out what is actually happening.
>
> I tried reader = new FileReader('sets.xml') with handlers for onloadend and
> onerror and that ignores me like the requestFileSystem call.
>
> I know I'm rambling but I've been beating my head on this brick wall for six
> hours and I'm exhausted.  Is there a concise tutorial short of reading the
> 2000 line dissertation at w3c <http://www.w3.org/TR/file-system-api/>?

DieHardDigitalDeveloper

unread,
Oct 5, 2011, 1:23:43 PM10/5/11
to phonegap
So, is there a way to add it to the resources in the /res folder and
access it through Android's native API?

On Oct 5, 9:17 am, Simon MacDonald <simon.macdon...@gmail.com> wrote:
> You can't use a FileReader as you xml file is not on the file system. It is
> part of your .apk so in Android parlance you'd need a FileDescriptor object
> in order to access it. This is how I would go about reading an .xml file
> from the assets directory without using jQuery:
>
>         *var* request = *new* XMLHttpRequest();
>
>         request.open("GET", "file:///android_asset/www/xml.xml");
>
>         request.onreadystatechange = *function*() {//Call a function when
> the state changes.
>
>             console.log("state = " + request.readyState);
>
>             *if*(request.readyState == 4) {
>
>                 console.log("*"+request.responseXML+"*");
>
>                }
>
>         }
>
>         console.log("asking for xml");
>
>         request.send();
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald
> > the 2000 line dissertation at w3c <http://www.w3.org/TR/file-system-api/>?

DieHardDigitalDeveloper

unread,
Oct 5, 2011, 1:32:27 PM10/5/11
to phonegap
Which of the phones are webkit (iPhone, Android, Blackberry)? Or are
they all webkit?

Steve Sobol

unread,
Oct 5, 2011, 1:43:51 PM10/5/11
to phon...@googlegroups.com
DieHardDigitalDeveloper wrote:
> Which of the phones are webkit (iPhone, Android, Blackberry)? Or are
> they all webkit?

iOS (Mobile Safari) definitely is. I believe Android also is. Don't know
about Blackberry.


Drew Walters

unread,
Oct 5, 2011, 1:57:00 PM10/5/11
to phon...@googlegroups.com
BlackBerry OS 6+ is webkit based. OS 5 and earlier are not.

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to

Simon MacDonald

unread,
Oct 5, 2011, 2:38:31 PM10/5/11
to phon...@googlegroups.com
You could stick your fies in the res folder but then you'd need to
write a plugin to get access to the resource folder. Try my code for
reading files out of the assets directory as you get a xml object in
your javascript when you use that approach.

Android, iOS, and BB 6+ use WebKit as their base for their web
broswers/web views.

bespired is incorrect in his assertion that WebKit cannot read from
file://. I know it can on mobile devices.

Simon Mac Donald
http://hi.im/simonmacdonald

DieHardDigitalDeveloper

unread,
Oct 5, 2011, 2:43:27 PM10/5/11
to phonegap
So I'm back to square one. @Simon MacDonald's AJAX solution is the
same as jquery's $.ajax that I've already tried to no good effect.

I had it in mind to offer additional sets as seperate packages and, if
what I'm reading in this group and other forums is correct, there's no
way to add files to the assets/www folder after the .apk is packaged.
Perhaps a solution would be to AJAX the xml file from my server and
save it in the persistant local file system on first run. That way,
additional sets could be added in the same way.

Ah, but then I run into the problem of it not being stand-alone. A
user would need internet access to get the xml file from my server
and, as I think of it, the .mp3 files in /assets/sets (which, btw, the
media API finds and plays quite handily).

BTW, thanks to all who have responded. I have a decade and a half in
web development but this is new, and exciting and frustrating as
heck! I'm happy to hear Adobe has acquired phonegap. Maybe it's time
to upgrade my CS3.

On Oct 5, 11:57 am, Drew Walters <deedu...@gmail.com> wrote:
> BlackBerry OS 6+ is webkit based. OS 5 and earlier are not.
>
>
>
>
>
>
>
> On Wed, Oct 5, 2011 at 12:43 PM, Steve Sobol <sjso...@justthe.net> wrote:
> > DieHardDigitalDeveloper wrote:
>
> >> Which of the phones are webkit (iPhone, Android, Blackberry)?  Or are
> >> they all webkit?
>
> > iOS (Mobile Safari) definitely is. I believe Android also is. Don't know
> > about Blackberry.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "phonegap" group.
> > To post to this group, send email to phon...@googlegroups.com
> > To unsubscribe from this group, send email to
> > phonegap+unsubscribe@**googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/**group/phonegap?hl=en?hl=en<http://groups.google.com/group/phonegap?hl=en?hl=en>

Simon MacDonald

unread,
Oct 5, 2011, 2:51:49 PM10/5/11
to phon...@googlegroups.com
Mark, did you try my solution in the emulator or on a device? I swear
it works quite well. If it isn't working what are you seeing in "adb
logcat"?

> phonegap+u...@googlegroups.com


> For more options, visit this group at

DieHardDigitalDeveloper

unread,
Oct 5, 2011, 3:44:36 PM10/5/11
to phon...@googlegroups.com
Simon, I haven't tried your solution as I had thought it would be the same as using jQuery's $.ajax function.  I will give it a try and let you know.  I'm taking care of an urgent site update for a client atm. 

This adb logcat is new to me.  Where do I go to view it's contents?  I've been wondering where to find the output from "console.log" as it doesn't show in Eclipses console or error log.  Is this where console.log outputs to?

Simon MacDonald

unread,
Oct 5, 2011, 3:58:53 PM10/5/11
to phon...@googlegroups.com
I'm concerned you may be using a version of jQuery that has an AJAX
bug in it. When you do an AJAX request from file:// you sometimes get
a responseCode of 0 when you are expecting 200. 0 is actually
perfectly fine in this case. So give my raw code a go as it takes
jQuery out of the equation.

Yes, if you have your Android dev kit in your path you can just do
"adb logcat" to see all the logs from the device and emulator,
including console.log.

http://developer.android.com/guide/developing/tools/adb.html#logcat

or use DDMS in Eclpse:

http://developer.android.com/guide/developing/debugging/ddms.html

Good luck with the customer issue.

DieHardDigitalDeveloper

unread,
Oct 5, 2011, 5:21:59 PM10/5/11
to phonegap
Simon, you are currently my favorite person in the world. Enjoy it
while it lasts, I tend to be fickle in these things.

After resolving the file reference and taking out the line break in
your comment, logcat shows *[object Document]*.

In the interests of going with what I know, I'll probably go back to
$.ajax but at least now I know what the problem was. jQuery has an
AJAX option statusCode for handling different returns. This works...

$.ajax({
url: 'file:///android_asset/Sets/set1.xml',
statusCode: {
0: function() {
console.log("*Status Code = 0");
},
200: function(response) {
setName= $(response).find('set').attr('setname');
console.log('*'+setName);
}
}
});

logcat shows *set1 at file:.....

So the AJAX call is returning 200. I don't know why that doesn't
equal success in jQuery but maybe the problem was my file wasn't
resolving and the AJAX was otherwise working. At any rate, this issue
is resolved. Thanks to all who responded.

On Oct 5, 1:58 pm, Simon MacDonald <simon.macdon...@gmail.com> wrote:
> I'm concerned you may be using a version of jQuery that has an AJAX
> bug in it. When you do an AJAX request from file:// you sometimes get
> a responseCode of 0 when you are expecting 200. 0 is actually
> perfectly fine in this case. So give my raw code a go as it takes
> jQuery out of the equation.
>
> Yes, if you have your Android dev kit in your path you can just do
> "adb logcat" to see all the logs from the device and emulator,
> including console.log.
>
> http://developer.android.com/guide/developing/tools/adb.html#logcat
>
> or use DDMS in Eclpse:
>
> http://developer.android.com/guide/developing/debugging/ddms.html
>
> Good luck with the customer issue.
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald
>
> On Wed, Oct 5, 2011 at 3:44 PM, DieHardDigitalDeveloper
>

Simon MacDonald

unread,
Oct 5, 2011, 5:35:03 PM10/5/11
to phon...@googlegroups.com

Hey,

I know I'm awesome. The people on the list know I'm awesome. Tell my wife. Please!

Seriously, glad my hint got you going.

Simon

Reply all
Reply to author
Forward
0 new messages