0

was hoping for some help with an xml file that my program generates and is downloaded to clients computer.

I'm generating a fight plan in an xml format that is then loaded into a flight simulator - which is not working. It appears to be an encoding issue with a coordinate in the xml file:

This coordinate and encoding works(from another program) (copy and paste in Bing maps to test):

S23° 5' 59.80",E149° 34' 46.14"

S23%B0%205%27%2059.80%22%2CE149%B0%2034%27%2046.14%22

This same coordinate and but different encoding (my xml output) doesn't work(copy and paste in Bing maps to test):

S25° 5′ 29.80",E149° 34′ 46.14"

S25%B0%A05%u2032%A029.80%22%2CE149%B0%A034%u2032%A046.14%22

My Javascript function where the XML file is created and downloaded:

function saveFlightPlan(document, flightPath_data) {


    filename = 'flight_plan.pln';
    var dep_dest_array = [];
    
    //build flight plan data to send to server so it can determine departure and arrival airports
    for (var i = 0; i < flightPath_data.length; i++) {

      airport_name = flightPath_data[i]['waypoint'];
      latlng_array = flightPath_data[i]['latLng'];
      lat = latlng_array[0];
      lng = latlng_array[1];
      coordinate = {'airport_name': airport_name, 'lat': lat, 'lon': lng};
      dep_dest_array.push(coordinate);
    }

    jsonified_data = JSON.stringify(dep_dest_array);

    fetch('http://localhost:5000/build_flightplan', {
      method: "POST",
      credentials: "include",
      body: jsonified_data,
      cache: "no-cache",
      headers: new Headers({
        "content-type": "application/json"
      })
    })
    .then(response => response.json())  
    .then(dep_dest_data => {
        
        
        console.log(JSON.stringify(dep_dest_data));
        console.log(dep_dest_data['dep_airport']);
        console.log(dep_dest_data['dest_airport']);       

        //build flight plan now that we have departure and arrival airports

        content = buildFlightPlan(flightPath_data, dep_dest_data);
   
        const a = document.createElement('a');
        const file = new Blob([content], {type: 'text/xml'});
        
        a.href= URL.createObjectURL(file);
        a.download = filename;
        a.click();
        
        URL.revokeObjectURL(a.href);

        var flight_plan_modal = document.getElementById("Flight_plan_modal");
        flight_plan_modal.classList.remove('show');
        // flight_plan_modal.setAttribute('aria-hidden', 'true');
        // flight_plan_modal.setAttribute('style', 'display: none');

    })
}

So, Im' at a loss on how to encode my XML file so it produces the first type of encoding (S23%B0%205%27%2059.80%22%2CE149%B0%2034%27%2046.14%22)??

Javscript where i create the xml document:

  // Create the XML document
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString('<?xml version="1.0" encoding="utf-8"?><SimBase.Document></SimBase.Document>', "application/xml");
...
...
   xmlDoc_string = new XMLSerializer().serializeToString(xmlDoc)
   xml_formatted = formatXml(xmlDoc_string);
   return xml_formatted;



Any help appreciated :)

UPDATE:

Actually, I think its got to do with this bit of code that converts the lat-long:

    a, // decimal value (ex. -14.23463)
    b, // boundary; accepts "90" (Latitude) or "180" (Longitude)
    c  // precision for seconds
  ){
    var 
      // get the direction indicator
      H='NSEW'[
        2*(b!=90)      // expressions in brackets are booleans, that get coerced into 0 or 1
        +(a<0)         // is the decimal value less than 0, coerced into 0 or 1
      ],  
      a=(a<0?-a:a)%b,  // convert value to absolute. shorten than Math.abs(a)
                       // also get the modulo of the value and the boundary
  
      D=0|a,          // Degress: get the integer value; like Math.floor(a)
      a=(a-D)*60,     // calulate the rest and multiply by 60
      M=0|a,          // Minutes
      a=(a-M)*60, 
      S=a.toFixed(c); // Seconds
  
      // return formatted values joined by non-breaking space
      return [H+D+'°',M+'′',S+'"'].join('\xA0')
}```

I changed the return line and now its working.  Thanks for your help :) 
```return [H+D+'°',M+'\'',S+'"'].join(' ')```




Richard
  • 63
  • 1
  • 7
  • The `′` character in your output is a different character from the `'` character in the coordinates that work. You also have a non-breaking space (`%A0`) in your output where the good coordinates have normal spaces (`%20`). I don't think this has anything to do with encoding or XML. It's just the wrong characters. – JLRishe Sep 25 '20 at 06:18
  • I know they are different but I'm not sure why. I'm reading the coordinates from CSV file. Maybe it would have something to do with that? They look like normal coordinates in the CSV file. : – Richard Sep 25 '20 at 06:27
  • If I use encodeURI on the string, the degree symbol translates to `%C2` (=capital A circumflex) but `%B0` is the degree symbol. It could be a characterset issue - try adding `;charset=UTF-8` to the `content-type` setting in the `fetch` block – ATD Sep 25 '20 at 06:30
  • 1
    Isn't the encoding parameter in the `open(...)` supposed to be `utf-8`? – ATD Sep 25 '20 at 06:33
  • This also sounds similar to: [encoding issue](https://stackoverflow.com/questions/42462764/javascript-export-csv-encoding-utf-8-issue) – ATD Sep 25 '20 at 06:35
  • Made the changes you suggest but still same issue: fetch('http://localhost:5000/build_flightplan', { method: "POST", credentials: "include", body: jsonified_data, cache: "no-cache", headers: new Headers({ "content-type": "application/json;charset=UTF-8" }) }) – Richard Sep 25 '20 at 06:42
  • Can you update that in your code. It definitely sounds like an charset issue – ATD Sep 25 '20 at 06:48
  • I think Ive figured out the issue. It was in a sction of code which converte the lattitude and longitude from decimal degrees: return [H+D+'°',M+'′',S+'"'].join('\xA0') – Richard Sep 25 '20 at 06:53
  • 1
    So, you should just be able to replace the degree symbol with `\xB0` ? – ATD Sep 25 '20 at 06:58
  • I just put in an empty space and some escape characters: return [H+D+'°',M+'\'',S+'"'].join(' '). Thanks for your help :) – Richard Sep 25 '20 at 07:40

0 Answers0