11.0.0 • Published 1 month ago

moddle-xml v11.0.0

Weekly downloads
26,486
License
MIT
Repository
github
Last release
1 month ago

moddle-xml

CI

Read and write XML documents described with moddle.

Usage

Get the libray via npm

npm install --save moddle-xml

Bootstrap

Create a moddle instance

import { Moddle } from 'moddle';
import {
  Reader,
  Writer
} from 'moddle-xml';

const model = new Moddle([ myPackage ]);

Read XML

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

const model; // previously created

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

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

// when
try {
  const {
    rootElement: cars,
    warnings
  } = await reader.fromXML(xml, rootHandler);

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

  console.log(cars);

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

} catch (err) {
  console.log('import error', err, err.warnings);
}

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 Writer(options);

var xml = writer.toXML(bar);

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

License

MIT

11.0.0

1 month ago

11.0.0-exp.4

1 year ago

11.0.0-exp.3

1 year ago

11.0.0-exp.2

1 year ago

11.0.0-exp.1

1 year ago

11.0.0-exp.0

1 year ago

10.1.0

1 year ago

9.0.6

2 years ago

10.0.0

2 years ago

9.0.5

3 years ago

9.0.4

3 years ago

9.0.3

4 years ago

8.0.8

4 years ago

8.0.7

4 years ago

9.0.2

4 years ago

9.0.1

4 years ago

8.0.6

4 years ago

8.0.5

4 years ago

9.0.0

4 years ago

8.0.3

4 years ago

8.0.2

4 years ago

8.0.1

5 years ago

8.0.0

5 years ago

7.5.0

5 years ago

7.4.1

5 years ago

7.4.0

5 years ago

7.3.0

5 years ago

7.2.3

6 years ago

7.2.2

6 years ago

7.2.1

6 years ago

7.2.0

6 years ago

7.1.0

6 years ago

7.0.0

6 years ago

6.0.0

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.1.4

6 years ago

4.1.3

6 years ago

4.1.2

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.0

6 years ago

3.0.0

6 years ago

2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.1

6 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.12.0

8 years ago

0.11.1

8 years ago

0.11.0

8 years ago

0.10.0

8 years ago

0.9.4

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

9 years ago

0.9.0

9 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.0

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago