unmarshalling - JavaScript equivalent of .NET XML Deserialization -


i'm looking javascript library can deserialize/unmarshal xml (strings or dom) javascript classes in way similar .net xmlserializer's deserialize method.

the functionality i'm looking for:

  1. classes defined javascript constructor functions.
  2. mapping between nodes , classes/properties configurable.
  3. deserialization result consists of instances of these classes.

for example, following xml:

<root textattribute='text1' numberattribute='1' attributetoignore1='ignored1' attributetoignore2='ignored2'>   <children>     <child>text2</child>     <child>text3</child>   </children>   <childtoignore>ignored3</childtoignore> </root> 

used javascript definitions similar these:

function rootclass() {   this.stringproperty = "";   this.integerproperty = 0;   this.collectionproperty = []; }  function childclass() {   this.stringproperty = ""; } 

should produce javascript objects similar following:

var result = new rootclass(); result.textproperty = "text1"; result.integerproperty = 1; result.collectionproperty = []; result.collectionproperty[0] = new childclass(); result.collectionproperty[0].textproperty = "text2"; result.collectionproperty[1] = new childclass(); result.collectionproperty[1].textproperty = "text3; 

an example of .net (c#) code same (see this .net fiddle working example):

public class program {     public static void main()     {         var result = serializer.deserialize();          console.writeline("text: {0}", result.stringproperty);         console.writeline("number: {0}", result.integerproperty);         console.writeline("enum: {0}", result.enumproperty);         console.writeline("child[0].value: {0}", result.collectionproperty[0].value);         console.writeline("other@[0]: {0}", result.otherattributes[0].innertext);         console.writeline("other*[0]: {0}", result.otherelements[0].innertext);     } }  public static class serializer {     public static rootclass deserialize()     {         var type = typeof (rootclass);          var serializer = new xmlserializer(type);          var xmlstring = @"                 <root textattribute='text1' numberattribute='1' enumattribute='item1' attributetoignore1='ignored1' attributetoignore2='ignored2'>                     <children>                         <child>text2</child>                         <child>text3</child>                     </children>                     <childtoignore>ignored3</childtoignore>                 </root>";          using (var stringreader = new stringreader(xmlstring))         {             return serializer.deserialize(stringreader) rootclass;         }     } }  [xmlroot("root")] public class rootclass {     [xmlattribute("textattribute")]     public string stringproperty;      [xmlattribute("numberattribute")]     public int integerproperty;      [xmlattribute("enumattribute")]     public enumeration enumproperty;      [xmlanyattribute]     public xmlattribute[] otherattributes;      [xmlarray("children")]     [xmlarrayitem("child")]     public collection<childclass> collectionproperty;      [xmlanyelement]     public xmlelement[] otherelements; }  public enum enumeration {     [xmlenum("item1")]     item1,      [xmlenum("item2")]     item2 }  public class childclass {     [xmltext]     public string value; } 

jsonix alexey valikov (source, guide) can deserialize xml javascript based on configurable mapping.


i've contributed code support deserializing custom classes using instance factories. make next release of jsonix (2.0.11).


var input = "<element1 attribute1='value1' />";  var class1 = function () {}; class1.prototype.tostring = function () {     return this.property1; };  var mapping = {     elementinfos: [{         elementname: "element1",         typeinfo: new jsonix.model.classinfo({             name: "element1",             instancefactory: class1,             propertyinfos: [                 new jsonix.model.attributepropertyinfo({                     name: "property1",                     attributename: "attribute1"                 })             ]         })     }] };  var context = new jsonix.context([mapping]); var unmarshaller = context.createunmarshaller(); var result = unmarshaller.unmarshalstring(input).value;  console.log(result.tostring()); // logs "value1" 

a longer working example on jsfiddle uses xml question.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -