smartxml/ts/index.ts

30 lines
837 B
TypeScript

import * as plugins from './smartxml.plugins.js';
export class SmartXml {
constructor() {}
public create = plugins.xmlbuilder2.create;
public parseXmlToObject<T = any>(xmlStringArg: string): T {
const parser = new plugins.fastXmlParser.XMLParser({
preserveOrder: true,
ignoreAttributes: false,
parseTagValue: true,
parseAttributeValue: true,
// ignoreDeclaration: true
});
const jsonObject = parser.parse(xmlStringArg);
return jsonObject as T;
}
public createXmlFromObject(jsObject: any): string {
const builder = new plugins.fastXmlParser.XMLBuilder({
ignoreAttributes: false,
attributeNamePrefix: '@_',
format: true,
indentBy: ' '
});
const xml = builder.build(jsObject);
return '<?xml version="1.0" encoding="UTF-8"?>\n' + xml;
}
}