const fxp = require('fast-xml-parser')
const J2XParser = require("fast-xml-parser").j2xParser;

/**
 * XML 工具类
 */
export class XmlUtils {

    /**
     * 将XML解析成 Object对象
     * @param xml 
     * @param parsingOpts 
     */
    static xmlToObj(xml: string, parsingOpts: any = undefined): any {
        if (!parsingOpts) {
            parsingOpts = {}
        }
        return fxp.parse(xml, parsingOpts)
    }


    /**
     * 将Object 对象转换成XML
     * @param xml 
     * @param parsingOpts 
     */
    static objToXML(obj: object, parsingOpts: any = {}): string {
        const parser = new J2XParser(parsingOpts);
        return parser.parse(obj)
    }
}
