1

I'm trying to create an XML formatted output in a textarea but have run into a asynch problems:

$(document).ready(function() {
    var geocoder;
    geocoder = new google.maps.Geocoder();
    $('#xmloutput').val('<?xml version="1.0" encoding="UTF-8"?>\n<parent>\n');
    var addresslist = 'one\ntwo\nthree';
    var addlines = addresslist.split('\n');
    $.each(addlines, function(name, value) {
        geocoder.geocode( { 'address': value}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                $('#xmloutput').val($('#xmloutput').val()+'<node>'+value+'</node>\n');
            }
        });
    });
    $('#xmloutput').val($('#xmloutput').val()+'</parent>');
});

I want this output:

<?xml version="1.0" encoding="UTF-8"?>
<parent>
<node>one</node>
<node>two</node>
<node>three</node>
</parent>

But I get this output because the geocoding takes a while...

<?xml version="1.0" encoding="UTF-8"?>
<parent>
</parent><node>one</node>
<node>two</node>
<node>three</node>

I've seen a lot of similar posts and the fix seemes to be chaining or callback, but I've not managed to get anything working yet. How should I approach this?

Thanks! Ben

2 Answers 2

2

Change your each loop and add the closing tag on last pass of the loop

/* first argument of `each` for an array is `index` which will increment on each pass*/
$.each(addlines, function(index,value) {
    geocoder.geocode({
        'address': value
    }, function(results,status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var newString=$('#xmloutput').val() + '<node>' + value + '</node>\n';           
            /* is this the last item? */
            if (index == addlines.length-1) {
                newString += '</parent>';
            }

            $('#xmloutput').val( newString)
        }
    });

}); ​

It is still possible for geocoder to return values out of sequence due to asynchronous nature of call to service. If this happens you may need to create a local object of all results and use a deffered to check all results received prior to loading the complete string

Sign up to request clarification or add additional context in comments.

Comments

0

try use .append() like code bellow:

$(document).ready(function() {
    var geocoder;
    geocoder = new google.maps.Geocoder();
    $('#xmloutput').val('<?xml version="1.0" encoding="UTF-8"?>\n<parent>\n');
    var addresslist = 'one\ntwo\nthree';
    var addlines = addresslist.split('\n');
    $.each(addlines, function(name, value) {
        geocoder.geocode( { 'address': value}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                $('#xmloutput').append('<node>'+value+'</node>\n');
            }
        });
    });
    $('#xmloutput').append('</parent>');
});

1 Comment

Hi, thanks for chipping in however that appends real html <node> tags to the textarea and doesn't change the value. I've used the solution below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.