smartxml/ts/index.ts

28 lines
790 B
TypeScript
Raw Normal View History

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 {
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);
return jsonObject as T;
2020-10-24 16:39:37 +00:00
}
public createXmlFromObject(jsObject: any): string {
const builder = new plugins.fastXmlParser.XMLBuilder({
2020-10-25 22:19:44 +00:00
ignoreAttributes: false,
attributeNamePrefix: '@_',
2020-10-26 00:44:26 +00:00
format: true,
indentBy: ' '
2020-10-25 22:19:44 +00: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
}
}