2023-10-20 17:38:44 +02:00
|
|
|
import * as plugins from './smartxml.plugins.js';
|
2020-10-24 15:47:12 +00:00
|
|
|
|
2020-10-24 16:39:37 +00:00
|
|
|
export class SmartXml {
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
public parseXmlToObject<T = any>(xmlStringArg: string): T {
|
2024-12-30 20:57:27 +01:00
|
|
|
const parser = new plugins.fastXmlParser.XMLParser({
|
|
|
|
preserveOrder: true,
|
|
|
|
ignoreAttributes: false,
|
|
|
|
parseTagValue: true,
|
|
|
|
parseAttributeValue: true,
|
|
|
|
// ignoreDeclaration: true
|
|
|
|
});
|
2023-10-20 17:38:44 +02:00
|
|
|
const jsonObject = parser.parse(xmlStringArg);
|
2024-12-30 20:57:27 +01:00
|
|
|
return jsonObject as T;
|
2020-10-24 16:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public createXmlFromObject(jsObject: any): string {
|
2024-12-30 20:57:27 +01:00
|
|
|
const builder = new plugins.fastXmlParser.XMLBuilder({
|
2020-10-25 22:19:44 +00:00
|
|
|
ignoreAttributes: false,
|
2024-12-30 20:57:27 +01:00
|
|
|
attributeNamePrefix: '@_',
|
2020-10-26 00:44:26 +00:00
|
|
|
format: true,
|
2024-12-30 20:57:27 +01:00
|
|
|
indentBy: ' '
|
2020-10-25 22:19:44 +00:00
|
|
|
});
|
2024-12-30 20:57:27 +01:00
|
|
|
const xml = builder.build(jsObject);
|
2020-10-26 00:44:26 +00:00
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>\n' + xml;
|
2020-10-24 16:39:37 +00:00
|
|
|
}
|
|
|
|
}
|