/** * xml工具类 * * @class IBizXMLWriter */ class IBizXMLWriter { /** * * * @type {any[]} * @memberof IBizXMLWriter */ public XML: any[] = []; /** * * * @type {any[]} * @memberof IBizXMLWriter */ public nodes: any[] = []; /** * * * @memberof IBizXMLWriter */ public State = ''; /** * * * @param {any} Str * @returns * @memberof XMLWriter */ public formatXML(Str) { if (Str) { return Str.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>').replace(/\n/g, '
').replace(/\r/g, '
'); } return ''; } /** * * * @param {any} Name * @returns * @memberof XMLWriter */ public beginNode(Name) { if (!Name) { return; } if (this.State === 'beg') { this.XML.push('>'); } this.State = 'beg'; this.nodes.push(Name); this.XML.push('<' + Name); } /** * * * @memberof XMLWriter */ public endNode() { if (this.State === 'beg') { this.XML.push('/>'); this.nodes.pop(); } else if (this.nodes.length > 0) { this.XML.push('</' + this.nodes.pop() + '>'); } this.State = ''; } /** * * * @param {any} Name * @param {any} Value * @returns * @memberof XMLWriter */ public attrib(Name, Value) { if (this.State !== 'beg' || !Name) { return; } this.XML.push(' ' + Name + '="' + this.formatXML(Value) + '"'); } /** * * * @param {any} Value * @memberof XMLWriter */ public writeString(Value) { if (this.State === 'beg') { this.XML.push('>'); } this.XML.push(this.formatXML(Value)); this.State = ''; } /** * * * @param {any} Name * @param {any} Value * @returns * @memberof XMLWriter */ public node(Name, Value) { if (!Name) { return; } if (this.State === 'beg') { this.XML.push('>'); } this.XML.push((Value === '' || !Value) ? '<' + Name + '/>' : '<' + Name + '>' + this.formatXML(Value) + '</' + Name + '>'); this.State = ''; } /** * * * @memberof XMLWriter */ public close() { while (this.nodes.length > 0) { this.endNode(); } this.State = 'closed'; } /** * * * @returns * @memberof XMLWriter */ public toString() { return this.XML.join(''); } }