smartxml/ts/index.ts

21 lines
593 B
TypeScript
Raw Normal View History

2020-10-24 15:47:12 +00:00
import * as plugins from './smartxml.plugins';
2020-10-24 16:39:37 +00:00
export class SmartXml {
constructor() {}
public parseXmlToObject<T = any>(xmlStringArg: string): T {
2020-10-24 21:03:51 +00:00
const jsonObject = plugins.fastXmlParser.parse(xmlStringArg);
2020-10-24 16:39:37 +00:00
return jsonObject;
}
public createXmlFromObject(jsObject: any): string {
2020-10-25 22:19:44 +00:00
const jsToXmlParser = new plugins.fastXmlParser.j2xParser({
ignoreAttributes: false,
2020-10-26 00:44:26 +00:00
attributeNamePrefix: "@_",
format: true,
indentBy: ' ',
2020-10-25 22:19:44 +00:00
});
2020-10-24 16:39:37 +00:00
const xml = jsToXmlParser.parse(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
}
}