简体   繁体   English

有什么办法可以用 JavaScript 创建一个带有 html 表单数据的 XML 文件吗?

[英]Is there any way to create an XML file with an html form data with JavaScript?

I got a massive html form.我得到了一个巨大的 html 表单。 I need the form data to be sent to an XML file every time I click the submit button.每次单击提交按钮时,我都需要将表单数据发送到 XML 文件。 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: "cbrice@femw.com" 5: "3213asdasdas" 6: "cmbriceno@gmail.com" 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: "cmbriceno@gmail.com" 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.我想要做的就是将此数组转换为具有正确结构的 XML。 Have been searching for hours for something similar but didn't find the solution.一直在寻找类似的东西,但没有找到解决方案。

Would tell something like this could be valid XML, but it is probably missing field names.会告诉这样的东西可能是有效的 XML,但它可能缺少字段名称。

 var data = {0: "2", 1: "105920808", 2: "Carlos Briceño", 3: "212135", 4: "cbrice@femw.com", 5: "3213asdasdas", 6: "cmbriceno@gmail.com", 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: "cmbriceno@gmail.com", 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM