0.1.7 • Published 8 months ago

@avanio/xml-mapper v0.1.7

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

Schema-based XML Parsing Utility Library

This is a library for parsing XML files based on a provided mapping schema. It is written in TypeScript and can be installed via npm.

Installation

To install the library, run the following command:

npm install --save @avanio/xml-mapper

Usage

type XmlData = {
	root: {
		string: string;
		number: number;
		date: Date;
		array: {id: number; item: number}[];
		object: {id: number; name: string; value: string};
	};
};

const xml = `<root>
    <string>string</string>
    <number>123</number>
    <date>2020-01-01T00:00:00.000Z</date>
    <array>
        <item id="1">1</item>
        <item id="2">2</item>
    </array>
    <object id="123">
        <name>name</name>
        <value>value</value>
    </object>
</root>`;

const doc = new DOMParser().parseFromString(xml);

const objectSchema: MappingSchema<XmlData['root']['object']> = {
	id: {mapper: rootAttrNumberValue('id'), required: true},
	name: {mapper: stringValue, required: true},
	value: {mapper: stringValue, required: true},
};

const itemSchema: MappingSchema<XmlData['root']['array'][number]> = {
	id: {mapper: rootAttrNumberValue('id'), required: true},
	item: {mapper: rootIntegerValue, required: true},
};

const dataSchema: MappingSchema<XmlData['root']> = {
	string: {mapper: stringValue, required: true},
	number: {mapper: integerValue, required: true},
	date: {mapper: dateValue, required: true},
	array: {mapper: arraySchemaValue(itemSchema), required: true},
	object: {mapper: objectSchemaValue(objectSchema), required: true},
};

const rootSchema: MappingSchema<XmlData> = {
	root: {mapper: objectSchemaValue(dataSchema), required: true},
};

const data: XmlData = rootParser(doc.documentElement, rootSchema);
0.1.7

8 months ago

0.1.6

10 months ago

0.1.0

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.4

12 months ago

0.1.3

12 months ago

0.1.5

11 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago