Java Script - Convert Object to XML and XML to Object while using custom names of attributes



I need to convert XML to Object and Object to XML in java script. (I can use a Json conversion in the middle of the conversion process)


But, I want to use my custom tag name and custom attribute name while converting, for example:


Person Object:



function PersonObject(firstName, lastName) {
this.FirstName = firstName;
this.LastName = lastName
}


Convert to XML like this:



<person f_name="First Name" l_name="Last Name" />


Something like saving metadata for every property in the object (in C# I did it with writing attribute: [XmlAttribute("f_name")] above the property).


I want to do it dynamically and not to give the objects properties those names because I want the continuing code to be readable.


So, How can I do it - Convert easily object to XML and vice versa while using custom names of attributes?


No comments:

Post a Comment