导入XML值?

Assuming I have a simple XML with only one item...

<myXML>
   <valuesItem
      name = 'name'
      age = 'age'
      gender = 'gender'
   />
</myXML>

...and want to import only the item's attribute values to be the new values of a jQuery object like this...

var myObject = {
        name: 'name',
        age: 'age',
        gender: 'gender'
};

...how shoud I proceed ???

I tried to achieve my purpose like this but I had no success so far:

var myObject = {};

$.get(myXML.xml, function(xmlData) {
      var xmlValues = {};

      $(xmlData).find('valuesItem').each(function() {
             var $item = $(this);
             xml Values = {
                    name: $item.attr('name'),
                    age: $item.attr('age'),
                    gender: $item.attr('gender')
             };
      });

      $.extend(myObject, xmlValues);
});

How can I make public the $.get() return results ?

You can do it like this.

var myObject = {};

$.ajax({
  type: 'get',
  url: 'myXML.xml',
  dataType: 'xml',
  success: function ( data ) {
    myObject = data;
  }  
});

HOWEVER, if you put the code that handles the xml directly after $.ajax() you will run into all sorts of errors because the call will not have completed.

Better that you put all of your logic inside the success function like so..

$.ajax({
  type: 'get',
  url: 'myXML.xml',
  dataType: 'xml',
  success: function ( data ) {
    //do stuff with data here
    //the variable "data" holds all of your xml, parsed into a JavaScript object
  }  
});

as success will not be called untill the server has responded to the request.

EDIT: you may be better served doing something like this for your particular usecase.

first define a function that takes an argument data and sets up the plugin

startPlugin = function ( data ) {
    var newoptions = {};
    newOptions.name = data.myXML.valuesItem.name;
    newOptions.age = data.myXML.valuesItem.age;
    newOptions.gender = data.myXML.valuesItem.gender;

    //start the plugin here with newOptions
}

then, pass that function to ajax as the success callback.

$.ajax({
  type: 'get',
  url: 'myXML.xml',
  dataType: 'xml',
  success: startPlugin  
});

also in $.ajax() you can set async to false to make the request synchronous but that's considered bad practice.

The problem here is that you are making an ajax call that is asynchronous. Either you use $.ajax and set the call not to be asynch or if you have to use the xml data, you must call a function in the success function of the $.get call.

for example you could do:

var myObject = {};

$.ajax({
  type: 'get',
  url: 'myXML.xml',
  dataType: 'xml',
  async: false, 
  success: function ( xmlData ) {

      $(xmlData).find('valuesItem').each(function() {
             var $item = $(this);
             myObject = {
                    name: $item.attr('name'),
                    age: $item.attr('age'),
                    gender: $item.attr('gender')
             };
      });
  }  
});
//here myObject is set because it waits for the call to finish