Git Product home page Git Product logo

js-xml-serializer's Introduction

About

js-xml-serializer is a Javascript serialization library that allows an arbitrary Javascript object to be serialized as XML according to a set of supplied rules and namespace definitions. The motivation behind this project was the author's inability to find an existing Javascript serialization method that supported namespaces and allowed enough control to allow sending complex Javascript objects as arguments to a SOAP web service.

Usage

In the nominal case, given a Javascript object like the following:

var obj = {
    attr1: "two",
    attr2: new MyClass( "hello" ),
    attr3: [ "three", "four" ]
};

We can produce a simple XML document:

<Object>
    <String>two</String>
    <MyClass>
        <String>hello</String>
    </MyClass>
    <Array>
        <String>three</String>
        <String>four</String>
    </Array>
</Object>

Using the following code:

serialize( obj );

In order to enable more control over serialization, we can create a structure outlining specific serialization rules to be applied to the object:

var rules = {
    Object: {
        __def__: { nodetype: "element", nodename: "obj", namespace: "http://example.org/" },
        attr1: { nodetype: "element", nodename: "first", namespace: "http://example.org/" }, 
        attr2: { nodetype: "element", nodename: "myc-attr2", namespace: "http://example.org/" } 
    },
    Array: {
        __def__: { nodetype: "element", nodename: "arr", namespace: "http://example.org/" }
    },
    String: {
        __def__: { nodetype: "element", nodename: "str", namespace: "http://example.org/" },
    },
    MyClass: {
        __def__: { nodetype: "element", nodename: "myc", namespace: "http://example.org/" }
    }
}

The rule denoted by __def__ will be applied to an appearance of the type in the object to be serialized if no other rule matches -- that is, if no rule that applies to a particular member of another type overrides it.

Namespace prefixes are specified as the following:

var namespaces = {
    "http://example.org/": "ex", 
}

The serialized XML output looks like this:

<ex:obj xmlns:ex='http://example.org/'>
    <ex:first>two</ex:first>
    <ex:myc-attr2>
        <ex:str>hello</ex:str>
    </ex:myc-attr2>
    <ex:arr>
        <ex:str>three</ex:str>
        <ex:str>four</ex:str>
    </ex:arr>
</ex:obj>

Status

js-xml-serializer is being developed as part of a larger project so it is only designed to solve serialization issues as-needed for that particular project. It is provided in the hopes that others may suggest extensions to cover cases that the author has not considered. This software is not being used by the author in production yet, and it is suggested that others test it fully before using it.

Future work

  • Clean up output: Output is not indented and some spurious (semantically insignificant) whitespace is emitted.
  • Provide default rules: Support for supplying a base rule set would make using the library more convenient. Provisions for merging rule sets could also prove useful.
  • Allow default namespace: Output explicitly defines a prefix for all namespaces. While semantically correct, it may produce more readable output to determine one namespace to use for the default.
  • Change def mechanism: the mechanism by which the default serialization rule is specified may not be the best solution. Comments are welcome for addressing this.
  • Don't pollute namespace: No wrapper object is provided around the basic serialize() function.

License

js-xml-serializer is copyright 2010 Dan Newcome and is provided under the MIT free software license. See the file LICENSE for the full text.

js-xml-serializer's People

Contributors

dnewcome avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.