简体   繁体   English

我们如何使用JavaScript或jQuery将HTML表单数据保存为xml格式?

[英]How can we save HTML form data into xml format using JavaScript or jQuery?

Here is a smiple html form,I want to get the data from it and convert into xml or get the data in xml format? 这是一个smiple html表单,我想从中获取数据并转换为xml或以xml格式获取数据?

<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    </head>
<body>
   <form action="" method="post">
        <p>
           <label for="firstname">First Name:</label>
            <input type="text" name="firstname" id="firstname">

            <label for="lastName">Last Name:</label>
            <input type="text" name="lastname" id="lastname">

        <button id="submitButton">submit</button>

    </body>
</form>
    </html>

If you want a pure javascript solution for downloading any content you can use the following function: 如果您想要一个纯JavaScript的解决方案来下载任何内容,您可以使用以下功能:

function downloadData(contentType,data,filename){

   var link=document.createElement("A");
   link.setAttribute("href",encodeURI("data:"+contentType+","+data));
   link.setAttribute("style","display:none");
   link.setAttribute("download",filename);
   document.body.appendChild(link); //needed for firefox
   link.click();
   setTimeout(function(){
    document.body.removeChild(link); //only to remove the temporal link
   },1000);
}

Where contentType where contentType

For creating XML data from a form you can use : 要从表单创建XML数据,您可以使用:

function fromToXml(form){
    var xmldata=['<?xml version="1.0"?>'];
      xmldata.push("<form>");
    var inputs=form.elements;
    for(var i=0;i<inputs.length;i++){
        var el=document.createElement("ELEMENT");
      if (inputs[i].name){
        el.setAttribute("name",inputs[i].name);
        el.setAttribute("value",inputs[i].value);
        xmldata.push(el.outerHTML);
      }

    }
    xmldata.push("</form>");
    return xmldata.join("\n");
}

And then, try to modify the format as you expected. 然后,尝试按预期修改格式。

See an example in https://jsfiddle.net/jmusfs9v/3/ 请参阅https://jsfiddle.net/jmusfs9v/3/中的示例

If you are intenerested in generating a well-formed XML output from javascript you can follow the article: http://archive.oreilly.com/pub/h/2127 如果您对从javascript生成格式良好的XML输出感兴趣,可以按照以下文章进行操作: http//archive.oreilly.com/pub/h/2127

Try this code - 试试这个代码 -

function getXml(){
    var elements = document.forms.myForm.elements;
    var xmlTemplate = '<?xml version="1.0"?> <formData>';
    for(var i =0; i< elements.length; i++){
        var element = elements[i];
        if(element.tagName=="INPUT"){
            xmlTemplate = xmlTemplate + '<'+element.name+'>' + element.value+'</'+element.name+'>';
        }


    }
    xmlTemplate = xmlTemplate +'</formData>';
   return xmlTemplate;
}

getXml();

暂无
暂无

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

相关问题 如何使用Javascript将表单输入数据保存到XML文件? - How to save form input data to a XML file using Javascript? 使用JavaScript将HTML表单数据保存到文件中 - Save HTML Form data into a File using JavaScript 如何使用 Javascript 或 jQuery 以 JSON 格式获取表单数据 - How to get form data in JSON format using Javascript or jQuery 如何获取HTML表单数据并使用JQuery写入XML? - How to take HTML form data and write to XML using JQuery? 如何使用Javascript将HTML表单数据输出到XML文件? - How to output HTML form data to a XML file using Javascript? 使用节点以javascript格式将.json格式的html格式数据保存在.json文件中 - Save html-form data in json format in a .json file using node and express with javascript 我们如何编辑SVG格式文件并另存为.html文件? - How can we edit the SVG format file and save as .html file? 如何使用jquery / javascript &amp;&amp;重置html中的表单数据,以及如何使用jquery将表单数据发送到电子邮件? - How to reset the form data in html using jquery/javascript && and also how to send the form data to an email using jquery? 如何使用jquery或javascript以HTML形式显示本地存储数据? - How to show localstorage data in HTML form using jquery or javascript? 我可以使用 JAVASCRIPT/jQuery 将表单中的输入保存到 HTML 中的 .txt,然后使用它吗? - Can I save input from form to .txt in HTML, using JAVASCRIPT/jQuery, and then use it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM