【问题标题】:JavaScript equivalent of .NET XML Deserialization.NET XML 反序列化的 JavaScript 等效项
【发布时间】:2014-08-18 12:09:26
【问题描述】:

我正在寻找一个 JavaScript 库,它可以以类似于 .NET XmlSerializer's Deserialize 方法的方式将 XML(字符串或 DOM)反序列化/解组为 JavaScript 类。

我正在寻找的功能:

  1. 类被定义为 JavaScript 构造函数。
  2. 节点和类/属性之间的映射是可配置的。
  3. 反序列化结果由这些类的实例组成。

例如,以下 XML:

<root textAttribute='text1' numberAttribute='1' attributeToIgnore1='ignored1' attributeToIgnore2='ignored2'>
  <children>
    <child>text2</child>
    <child>text3</child>
  </children>
  <childToIgnore>ignored3</childToIgnore>
</root>

用于与这些类似的 JavaScript 定义:

function RootClass() {
  this.stringProperty = "";
  this.integerProperty = 0;
  this.collectionProperty = [];
}

function ChildClass() {
  this.stringProperty = "";
}

应该产生类似于以下的 JavaScript 对象:

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;

执行相同操作的 .NET (C#) 代码示例如下(参见this .NET Fiddle for a 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) as 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;
}

【问题讨论】:

  • @Srinivasan__ 是的,但该帖子中提到的库都不允许使用预定义的 JavaScript 类或忽略特定节点。他们不允许我见过的任何类型的映射配置。如果有,请您帮忙解释和举例。

标签: javascript unmarshalling xml-deserialization jsonix


【解决方案1】:

Jsonix by Alexey Valikov (source, guide) 可以基于可配置的映射将 XML 反序列化为 JavaScript。


我贡献了代码来支持deserializing custom classes using instance factories。这有望使其成为 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"

JSFiddle 上更长的working example 使用来自问题的 XML。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 2015-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多