Skip to content

xml-modelXML ↔ TypeScript

Bidirectional XML and object conversion using decorators and runtime type information.

What is xml-model?

xml-model lets you define TypeScript classes that map directly to XML documents. Annotate your class with @Model() and its properties with @Prop(), then convert in either direction with a single method call.

ts
import { Model, Prop } from "xml-model";

@Model({
  fromXML({ xml, properties }) {
    const obj = new Book();
    obj.title = properties.title as string;
    obj.year = properties.year as number;
    return obj;
  },
})
class Book {
  @Prop() title: string = "";
  @Prop() year: number = 0;
}

const model = getModel(Book);

// XML → object
const book = model.fromXML(`<book><title>Dune</title><year>1965</year></book>`);

// object → XML
const xml = model.toXML(book);

Class and property names are automatically converted to kebab-case XML tags (BookChapterbook-chapter, publishedAtpublished-at). Runtime type information is provided by typescript-rtti, so no manual type annotations are needed in most cases.