JavaScript эквивалент десериализации.NET XML

Я ищу библиотеку JavaScript, которая может десериализовать / демаршировать XML (строки или DOM) в классы JavaScript способом, аналогичным методу десериализации.NET XmlSerializer.

Функциональность, которую я ищу:

  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#), который делает то же самое, будет выглядеть примерно так (см. Этот.NET Fiddle для рабочего примера):

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;
}

1 ответ

Решение

Jsonix от Алексея Валикова ( источник, руководство) может десериализовать XML в JavaScript на основе настраиваемого отображения.


Я добавил код для поддержки десериализации пользовательских классов с использованием фабрики экземпляров. Надеемся, что это войдет в следующую версию 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 использует XML из вопроса.

Другие вопросы по тегам