export default class XMLBuilder { private name : string ; private value ? : string ; private attributes ? : Record < string , string > ; private elements : XMLBuilder [ ] ; constructor ( name , attributes ? , value ? ) { this . name = name ; this . attributes = attributes ; this . value = value ; this . elements = [ ] ; } elem ( name , value ? , attributes ? ) { const el = new XMLBuilder ( name , attributes , value ) ; this . elements . push ( el ) ; return el ; } add ( element ) { this . elements . push ( element ) ; } build ( ) { const xml = [ ] ; xml . push ( `<?xml version="1.0" encoding="utf-8"?>` ) ; xml . push ( this . writeElement ( this ) ) ; return xml . join ( '' ) ; } private writeElement ( element ) { const xml = [ ] ; const attrs = [ ] ; for ( const key in element . attributes ) attrs . push ( `${ key }="${ element . attributes [ key ] }"` ) ; if ( attrs . length > 0 ) { xml . push ( `<${ element . name } ${ attrs . join ( ' ' ) }>` ) ; } else { xml . push ( `<${ element . name }>` ) ; } if ( element . value ) xml . push ( element . value ) ; for ( const item of element . elements ) { xml . push ( this . writeElement ( item ) ) ; } xml . push ( `</${ element . name }>` ) ; return xml . join ( '' ) ; } }