xml-writer.ts 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/**
 * xml工具类
 * 
 * @export
 * @class XMLWriter
 */
export class XMLWriter {
    public XML: any[] = [];
    public nodes: any[] = [];
    public State = '';

    /**
     * 
     * 
     * @param {any} Str 
     * @returns 
     * @memberof XMLWriter
     */
    public formatXML(Str: any) {
        if (Str) {
            return Str.replace(/&/g, '&amp;').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '&#xA;').replace(/\r/g, '&#xD;');
        }
        return '';
    }

    /**
     * 
     * 
     * @param {any} Name 
     * @returns 
     * @memberof XMLWriter
     */
    public beginNode(Name: any) {
        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: any, Value: any) {
        if (this.State !== 'beg' || !Name) {
            return;
        }
        this.XML.push(' ' + Name + '="' + this.formatXML(Value) + '"');
    }

    /**
     * 
     * 
     * @param {any} Value 
     * @memberof XMLWriter
     */
    public writeString(Value: any) {
        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: any, Value: any) {
        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(''); }
}