XML for Form forms (Javascript)

Source: Internet
Author: User
Tags html form

How do I generate the corresponding XML from an HTML form? It may be used when we create a simple Web service interface for the application, to look at this function:

/*设置一个命名空间防止冲突*/if(typeofjscript == ‘undefined‘) {  jscript = function() { }}jscript.form = function() { }/** * This function takes an HTML form and constructs an XML document from it, * using the specified root element. *(这个函数把 HTML 表单中的信息转换为 XML document) * @param  inForm        A reference ot the HTML form object to serialize. * @param  inRootElement The root element the XML dccument should use. * @return               A string of XML constructed from serializing the *                       specified form.(此参数为 XML document 的根元素) */jscript.form.formToXML = function(inForm, inRootElement) {  if(inForm == null) {    returnnull;  }  if(inRootElement == null) {    returnnull;  }  varoutXML = "<"+ inRootElement + ">";  vari;/*开始处理*/  for(i = 0; i < inForm.length; i++) {    var ofe = inForm[i];    varofeType = ofe.type.toUpperCase();    varofeName = ofe.name;    varofeValue = ofe.value;    /*处理不同的 input 框及 select 和 textarea,"SELECT-ONE" 为单选模式*/    if (ofeType == "TEXT"|| ofeType == "HIDDEN"||      ofeType == "PASSWORD"|| ofeType == "SELECT-ONE"||      ofeType == "TEXTAREA") {      outXML += "<"+ ofeName + ">"+ ofeValue + "</"+ ofeName + ">"    }    /*处理 select 的多选(multiple)模式*/    if(ofeType == "SELECT-MULTIPLE"){        outXML += "<"+ ofeName + ">";        for(j = 0; j < ofe.options.length; j ++){            outXML += "<option"+ (j + 1) + ">";            outXML += "<text>"+ ofe.options[j].innerHTML + "</text>";            outXML += "<value>"+ ofe.options[j].value + "</value>";            outXML += "</option"+ (j + 1) + ">";        }        outXML += "</"+ ofeName + ">";    }    /*处理单选框(radio)*/    if(ofeType == "RADIO"&& ofe.checked == true) {      outXML += "<"+ ofeName + ">"+ ofeValue + "</"+ ofeName + ">"    }    /*处理多选框*/    if(ofeType == "CHECKBOX") {      if(ofe.checked == true) {        cbval = "true";      } else{        cbval = "false";      }      outXML = outXML + "<"+ ofeName + ">"+ cbval + "</"+ ofeName + ">"    }    outXML += "";  }  outXML += "</"+ inRootElement + ">";  returnoutXML;} // End formToXML().

To look at a form, try the effect of this function:

<formid="testForm">    <input type="text" name="firstName" value="Jack"><br>    <inputtype="text" name="lastName" value="Redd"><br>    <selectmultiple id="numComputers" name="numComputers">      <optionvalue="1">I own one computer</option>      <optionvalue="2">I own two computers</option>      <optionvalue="2+">I own two or more computers</option>    </select></form>

To invoke a function from a table cell:

jscript.form.formToXML(document.getElementById(‘testForm‘),‘Person‘)

Take a look at the results of its processing:

<Person>    <firstName>Jack</firstName>    <lastName>Redd</lastName>    <numComputers>        <option1>            <text>I own one computer</text>            <value>1</value>        </option1>        <option2>            <text>I own two computers</text>            <value>2</value>        </option2>        <option3>            <text>I own two or more computers</text>            <value>2+</value>        </option3>    </numComputers></Person>

Exactly what format of XML information is generated depends on the requirements of the application, and then by modifying the function to achieve the goal, this is just an example. :->

XML for Form forms (Javascript)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.