1

I have JavaScript object which is formatted as JSON using this code:

obj.push({      
    lat: (lat / 1e5).toFixed(5),
    lon: (lon / 1e5).toFixed(5),
    ele: "0"
});

This object/array can contain potentially hundereds of indiviual objects/properties which look like this:

 [{"lat":"32.71883","lon":"-16.76118","ele":"0"},{"lat":"32.71882","lon":"-16.76138","ele":"0"},{"lat":"32.71881","lon":"-16.76159","ele":"0"},{"lat":"32.71880","lon":"-16.76179","ele":"0"},{"lat":"32.71879","lon":"-16.76199","ele":"0"},{"lat":"32.71878","lon":"-16.76220","ele":"0"}....]

I'd like to convert this object to XML using the correct element tags in the following format:

<trkpt lat="32.7188300" lon="-16.7611800">   // note how this lumps together two separate properties into a single element
 <ele>0</ele>
</trkpt>

I've found a snippet of code to tranform the object:

function objectToXml(object) {
        var xml = '';
        for (var prop in object) {
            if (!object.hasOwnProperty(prop)) {
                continue;
            }
            if (object[prop] == undefined)
                continue;
            xml += "<" + prop + ">";
            if (typeof object[prop] == "object")
                xml += objectToXml(new Object(object[prop]));
            else
                xml += object[prop];
            xml += "<!--" + prop + "--\>";
        }
        return xml;
    }

This certainly converts the object to some extent, however, it tries to create a key for each property set, an concatenates the properties into one string.

  <0>32.71883-16.761180<1>32.71882-16.761380<2>32.71881-16.761590<3>32.71880-16.761790<4>.....

Any suggestions on how I can use this function to correctly format the XML as described?

2
  • 1
    Hi. I don't want to be rude but have you tried first to tweak the objectToXml function before asking for help ? :) Commented Sep 13, 2018 at 10:18
  • @Thomas Lombart - I have indeed, but I just can't get it to do what I want :( Commented Sep 13, 2018 at 10:20

1 Answer 1

1

You could do that with string templates

const data = [{
  "lat": "32.71883",
  "lon": "-16.76118",
  "ele": "0"
}, {
  "lat": "32.71882",
  "lon": "-16.76138",
  "ele": "0"
}, {
  "lat": "32.71881",
  "lon": "-16.76159",
  "ele": "0"
}, {
  "lat": "32.71880",
  "lon": "-16.76179",
  "ele": "0"
}, {
  "lat": "32.71879",
  "lon": "-16.76199",
  "ele": "0"
}, {
  "lat": "32.71878",
  "lon": "-16.76220",
  "ele": "0"
}]



const toXml = (data) => {
  return data.reduce((result, el) => {
   return result + `<trkpt lat="${el.lat}" lon="${el.lon}"><ele>${el.ele}</ele></trkpt>\n`
  }, '')
}

console.log(toXml(data))

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

6 Comments

This works perfectly if executed in the code snippet runner. However, if I embed this in HMTL and run it using document.write(toXml(data)); I just get a bunch of zeros 0 0 0 0 0 0 0 0
Instead of document.write, try : document.body.textContent += toXml(data))
This shows me the converted string which is great, but also prints the contents of the entire JS script too - do you know how I can suppress this?
my toXml function only returns the string you asked to be generated, the , so I have no idea what you are talking about by "entire JS script".
Apologies, my fault for lack of clairty - I'm embedding your code snippet into an html file so I can see what's going on. If I use document.body.textContent += toXml(data) I can see the parsed/converted string which is exactly what I'm after, but also see the raw JavaScript code on the html page
|

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.