Coder Social home page Coder Social logo

moddle-xml's Introduction

moddle-xml

Build Status

Read and write XML documents described with moddle.

Usage

Get the libray via npm

npm install --save moddle-xml

Bootstrap

Create a moddle instance

var Moddle = require('moddle'),
    ModdleXML = require('moddle-xml');

var model = new Moddle([ myPackage ]);

Read XML

Use the reader to parse XML into an easily accessible object tree:

var model; // previously created

var xml =
  '<my:root xmlns:props="http://mypackage">' +
    '<my:car id="Car_1">' +
      '<my:engine power="121" fuelConsumption="10" />' +
    '</my:car>' +
  '</my:root>';

var reader = new Reader(model);
var rootHandler = reader.handler('my:Root');

// when
reader.fromXML(xml, rootHandler, function(err, cars, context) {

  if (err) {
    console.log('import error', err);
  } else {

    if (context.warnings.length) {
      console.log('import warnings', context.warnings);
    }

    console.log(cars);

    // {
    //  $type: 'my:Root',
    //  cars: [
    //    {
    //      $type: 'my:Car',
    //      id: 'Car_1',
    //      engine: [
    //        { $type: 'my:Engine', powser: 121, fuelConsumption: 10 }
    //      ]
    //    }
    //  ]
    // }
  }
});

Write XML

Use the writer to serialize the object tree back to XML:

var model; // previously created

var cars = model.create('my:Root');
cars.get('cars').push(model.create('my:Car', { power: 10 }));

var options = { format: false, preamble: false };
var writer = new ModdleXML.Writer(options);

var xml = writer.toXML(bar);

console.log(xml); // <my:root xmlns:props="http://mypackage"> ... <my:car power="10" /></my:root>

License

MIT

moddle-xml's People

Contributors

nikku avatar iso50 avatar sebastianstamm avatar

Watchers

 avatar James Cloos 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.