Unable to load xml file with jquery in phonegap project from assets path


Answers

Try putting your xml file in "www" folder and then you should be possible to access the file with just the filename.

Also, see the autocomplete example : http://jqueryui.com/autocomplete/#xml In the demo they have directly accessed "london.xml"

$.ajax({
  url: "london.xml",
  dataType: "xml",
  success: function( xmlResponse ) {
  var data = $( "geoname", xmlResponse ).map(function() {
  //alert($('name', this).text());
  return {
    value: $( "name", this ).text() + ", " +
            ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
    id: $( "geonameId", this ).text()
  };
 });

Hopefully it will work.

Question

I have huge problem with loading xml file from assets path by jQuery mobile in phonegap project.

I need to load an xml file. File is placed in the root of my project. Problem is with ajax url: "language.xml". Here is my code:

var language = 'english';
var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;
$.ajax({
    url: "language.xml",
    success: function(xml) {
        $(xml).find('translation').each(function(){
            var id = $(this).attr('id');
            var text = $(this).find(language).text();
            if(text.match(regEx)){
              $("." + id).replaceWith('<a href="mailto:'+text+'" data-role="button" data-inline="true" data-theme="d" rel="external" data-mini="true">'+text+'</a>');
            }
            else{
              $("." + id).html(text);
            }
        });
    }
}); 

When I use the absolute path, I am able to load this file adding url: file:///android_asset/www/language.xml

This is good only for Android assets. But I need the correct path for iOS too.

Is it possible to link URL path absolutly/relativly by jQuery to be able to load file in Android/iOS devices?

UPDATE Code above is correct. Fail was in testing within the desktop browser. Project works fine on Android and iOS with relative paths.

There's no need to add absolute path like file:///android_asset/www/ for Android or file:///var/mobile/Applications/7D6D107B-D9DC-479B-9E22-4847F0CA0C40/YourApplication.app/www/ for iOS .





Tags