smartxml/ts/index.ts

22 lines
640 B
TypeScript
Raw Permalink Normal View History

2023-10-20 15:38:44 +00: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 {
2023-10-20 15:38:44 +00:00
const parser = new plugins.fastXmlParser.XMLParser();
const jsonObject = parser.parse(xmlStringArg);
2020-10-24 16:39:37 +00:00
return jsonObject;
}
public createXmlFromObject(jsObject: any): string {
2023-10-20 15:38:44 +00:00
const jsToXmlParser = new plugins.fastXmlParser.XMLBuilder({
2020-10-25 22:19:44 +00:00
ignoreAttributes: false,
2020-10-26 00:44:26 +00:00
attributeNamePrefix: "@_",
format: true,
indentBy: ' ',
2020-10-25 22:19:44 +00:00
});
2023-10-20 15:38:44 +00:00
const xml = jsToXmlParser.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
}
}