Socket
Book a DemoInstallSign in
Socket

typed-xml-builder

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-xml-builder

Type-safe xml paser and builder based in xsd for Typescript environments

0.3.1-beta.0
latest
npmnpm
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

typexml

Type-safe XML parser and builder based in XSD file for Typescript environments

installation

npm i typexml or yard add typexml

codegen

In your project, after installing typexml, run the following to generate types for your XSD:

npx typexml generate ${path-to-xsd-file.xsd}

This should generate interfaces.ts.

interfaces.ts holds the collection of types extracted from the provided XSD schema. It can be imported anywhere you want to generate a compliant XML.

usage

build

Let's say you want to generate the element "foo" defined in your XSD file

import { createBuilder } from 'typemxml';
import { Schema } from '/path/to/interfaces.ts';

const schema: Schema = {
    foo: {
        // fill in foo props with type safety...
        bar: 'bar'
        ...props,
    },
};

const build = createBuilder('path-to-xsd-file.xsd');

const xml: string = build(schema, 'foo');

writeFileSync('foo.xml', xml);

validation

You can validate the XML with the help of xsd-validator package (npm i xsd-validator) (or any other library of your choice...)

import { readFileSync } from 'fs';
import validateSchema, { ValidationError } from 'xsd-schema-validator';

const xsd = readFileSync('path-to-xsd-file.xsd').toString();

const errors: true | ValidationError[] = validateSchema(xml, xsd);

if (errors instanceof Array) {
    errors.forEach(e => console.error(e));
    throw new Error('Invalid XML');
}

parse

It also comes with a parser in case you need

  • With XSD/interface compliant parsing:
import { readFileSync } from 'fs';
import { createParser } from 'typemxml';
import { Foo } from '/path/to/interfaces.ts';

const xml = readFileSync('path-to-xml-file.xml').toString();

const parse = createParser<Foo>('path-to-xsd-file.xsd');

// whatever element was parsed is guaranteed to comply with both XSD/interface
const foo: Foo = parse(xml);

console.log(foo.bar.length);
  • Without:
import { readFileSync } from 'fs';
import { createParser } from 'typemxml';

const xml = readFileSync('path-to-xml-file.xml').toString();

const parse = createParser();

// parse as any other regular xml parser returning a loosely typed Record<string, any>
const foo: Record<string, any> = parse(xml);

console.log(foo.bar?.length);

Keywords

xsd

FAQs

Package last updated on 28 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.