xml-class-transformer
Ƭ XmlClass: () => any
• new XmlClass(): any
The XML class's constructor should not require any arguments. This is because the xml-class-transformer needs to be able to construct them when it needs to. And if the constructor relies on the arguments then it will crash.
Note that it is okay and even recommended to give your classes a constructor like this:
class SomeXmlElement {
...
constructor(seed?: SomeXmlElement) {
Object.assign(this, seed || {})
}
}
note that the seed
argument is optional. Such a constructor
gives you a way to create instances with passed values and also
enable the library to construct them without passing any arguments.
any
Ƭ XmlPrimitiveType: typeof String
| typeof Number
| typeof Boolean
| typeof BigInt
Ƭ XmlType: XmlPrimitiveType
| XmlClass
▸ XmlAttribute(opts
): PropertyDecorator
Class property decorator. For more details on options see XmlAttributeOptions
Example
// a basic example
class SomeXmlElement {
@XmlAttribute({ name: 'attributeName', type: () => String })
attributeName: string;
}
Name | Type |
---|---|
opts |
XmlAttributeOptions |
PropertyDecorator
▸ XmlChardata(opts
): PropertyDecorator
The property will be parsed and serialized as a character data. The "type" parameter can only be a primitive type: String, Number, Boolean.
\@XmlElem({ name: 'Comment' })
class Comment {
\@XmlChardata({ type: () => String })
text: string;
\@XmlAttribute({ type: () => String, name: 'lang' })
language: string;
constructor(d?: Comment) {
Object.assign(this, d || {});
}
}
classToXml(
new Comment({
text: 'This is awesome',
language: 'en',
}),
)
Output:
<Comment lang="en">This is awesome</Comment>
Name | Type |
---|---|
opts |
XmlChardataOptions |
PropertyDecorator
▸ XmlChildElem(opts
): PropertyDecorator
Class property decorator.
Example
class SomeElement {
@XmlChildElem({ type: () => String })
stringElem: string;
@XmlChildElem({ name: 'someOtherName', type: () => Number })
numberElem: string;
@XmlChildElem({ type: () => NestedElem })
nestedElem: NestedElem;
}
Name | Type |
---|---|
opts |
XmlChildElemOptions |
PropertyDecorator
▸ XmlComments(): PropertyDecorator
This decorator, when used on a class property, collects all the comments from the provided XML, turns them into an array of strings and puts them into that property. And vice-versa: at serialization that array of strings gets serialized to set of comments in the resulting XML.
Example
class SomeElement {
\@XmlComments()
comments?: string[];
}
classToXml(
new SomeElement({
comments: ['some comment', 'some other comment']
})
)
Output:
<SomeElement>
<!-- some comment -->
<!-- some other comment -->
</SomeElement>
PropertyDecorator
▸ XmlElem(opts?
): ClassDecorator
A class decorator. It can be omitted, but only if at least one Xml* property decorator is used on it's properties.
Example
@XmlElem()
class EmptyXmlElement {}
@XmlElem({ name: 'some-xml-element' })
class SomeXmlElement {
@XmlChildElem()
child: string;
}
@XmlElem({ xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/' })
class SomeXmlElement {
@XmlChildElem()
child: string;
}
Name | Type |
---|---|
opts? |
XmlElemOptions |
ClassDecorator
▸ classToXml(entity
, options?
): string
Name | Type |
---|---|
entity |
any |
options? |
ClassToXmlOptions |
string
src/transform-class-to-xml.ts:6
▸ xmlToClass<T
>(xml
, _class
): InstanceType
<T
>
Name | Type |
---|---|
T |
extends XmlClass |
Name | Type |
---|---|
xml |
string |
_class |
T |
InstanceType
<T
>