0

I got a massive html form. I need the form data to be sent to an XML file every time I click the submit button. I already have each form input valueinside a big array which looks like this:

0: "2" 1: "105920808" 2: "Carlos Briceño" 3: "212135" 4: "[email protected]" 5: "3213asdasdas" 6: "[email protected]" 7: "87047866" 8: "87047866" 9: "1" 10: "55" 11: "8" 12: "Urbanizacion Las Lomas" 13: "Alto de Guadalupe, Urbanización las Lomas" 14: "324131" 15: "pass1 " 16: "444444444444" 17: "1231" 18: "321" 19: "32" 20: "1" 21: "1" 22: "98" 23: "Carlos" 24: "0" 25: "[email protected]" 26: "24402969" 27: "87047866" 28: "0" 29: "0" 30: "0" 31: "" 32: "" 33: "1231132" 34: "Cambiar AE" 35: "02" 36: "CRC" 37: "01" 38: "565.28" 39: "01" 40: (9) ["1", "123456", "1", "I", "100000", "Linea 1", "", "", "08"] 41: (9) ["1", "10101", "1", "Alc", "20000", "linea 2 ", "10", "", "08"] 42: "This is a test" 43: "118000" 44: "" 45: "" 46: "" 47: "" 48: "" 49: "118000" 50: "" 51: "" 52: "2000" 53: "118000" 54: "116000" 55: "" 56: "" 57: "133340"

All I want to do is to convert this array to a XML with the propper structure. Have been searching for hours for something similar but didn't find the solution.

5
  • 1
    I believe that your question is related to: stackoverflow.com/questions/48788722/… Commented Mar 9, 2020 at 20:23
  • Should that be inside an array? Would it not be better to store it as object? Then use some library like this: npmjs.com/package/xml-js (just the first result from google) Commented Mar 9, 2020 at 20:23
  • if you already have the array, all you need is to run through the array with a simple for loop and structure your data the way you want it in xml (add <item> around it or whatever). add a header at the top, output it to the browser or a file and you're done. read up on xml file structure on w3schools.com/xml - no library needed. Commented Mar 9, 2020 at 20:27
  • Thank you all guys, I'll try every of the solutions and let you know which worked the best for me Commented Mar 9, 2020 at 20:30
  • I'm still triyin to do it. don't you know the way? Commented Mar 10, 2020 at 21:21

1 Answer 1

0

Would tell something like this could be valid XML, but it is probably missing field names.

var data = {0: "2", 1: "105920808", 2: "Carlos Briceño", 3: "212135", 4: "[email protected]", 5: "3213asdasdas", 6: "[email protected]", 7: "87047866", 8: "87047866", 9: "1", 10: "55", 11: "8", 12: "Urbanizacion Las Lomas", 13: "Alto de Guadalupe, Urbanización las Lomas", 14: "324131", 15: "pass1 ", 16: "444444444444", 17: "1231", 18: "321", 19: "32", 20: "1", 21: "1", 22: "98", 23: "Carlos", 24: "0", 25: "[email protected]", 26: "24402969", 27: "87047866", 28: "0", 29: "0", 30: "0", 31: "", 32: "", 33: "1231132", 34: "Cambiar AE", 35: "02", 36: "CRC", 37: "01", 38: "565.28", 39: "01", 40: ["1", "123456", "1", "I", "100000", "Linea 1", "", "", "08"], 41: ["1", "10101", "1", "Alc", "20000", "linea 2 ", "10", "", "08"], 42: "This is a test", 43: "118000", 44: "", 45: "", 46: "", 47: "", 48: "", 49: "118000", 50: "", 51: "", 52: "2000", 53: "118000", 54: "116000", 55: "", 56: "", 57: "133340" };
function print2XML(data, y) {
    var res = '';
    for(var x in data) {
        var idx = y ? (y + '.' + x) : x;
        if (data[x].constructor == Array) {
            res += '<Item' + idx + '>\n' + print2XML(data[x], idx) + '</Item' + idx + '>\n';
        }
        else res += '<Item' + idx + '>' + data[x] + '</Item' + idx + '>\n';
    }
    return res;
}
console.log('<yourXMLdata>\n' + print2XML(data) + '</yourXMLdata>');
document.write(('<yourXMLdata>\n' + print2XML(data) + '</yourXMLdata>').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br>'));
document.close();

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

Comments

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.