form_to_xml

by raybiss

HTML

<h3>Enter some information</h3>
<form action="javascript:getFormXml()">
    Username: <input type="text" name="user"/><br/>
    First name: <input type="text" name="firstname"/><br/>
    Last name: <input type="text" name="lastname"/><br/>
    Password: <input type="password" name="pwd"/><br/>
    <hr/>
    <input type="radio" name="sex" value="male"/>Male<br/>
    <input type="radio" name="sex" value="female"/>Female<br/>
     <hr/>
    <input type="checkbox" name="vehicle" value="Bike"/>I have a bike<br/> 
    <input type="checkbox" name="vehicle" value="Car"/>I have a car<br/>
     <hr/>
    <input name="submit" type="submit" value="Submit"/> 
</form>

CSS

html{font-family:sans-serif;}

JavaScript

function getFormXml(){
    var sXml;
    var sType;
    var sValue;
    sXml = "<xml>" + "\n"
    $( "form input" ).each(function() {
        sType = $(this).attr("type");
        sValue = $(this).val();
        if(sType == "checkbox" || sType == "radio") {
            sXml = sXml + "<input type='" + $(this).attr("type") + "' checked='" + $(this).prop("checked") + "'>" + sValue + "</input>" + "\n";
        } else {
            sValue = $(this).val();
            sXml = sXml + "<input type='" + $(this).attr("type") + "'>" + sValue + "</input>" + "\n";
        }   
    });
    sXml = sXml + "</xml>" + "\n";
    alert(sXml);
}