param xml return export function xmlToJson ( xml ) { const obj = { } ; if ( ! xml ) { return null ; } if ( typeof xml === 'string' ) { xml = new DOMParser ( ) . parseFromString ( xml , 'text/xml' ) ; } if ( xml . nodeType === 3 ) { if ( xml . nodeValue . trim ( ) === '' ) { return null ; } return xml . nodeValue ; } if ( xml . nodeType === 1 ) { const el = xml as Element ; const attrs = el . attributes ; for ( let i = 0 , len = attrs . length ; i < len ; i ++ ) { const attr = attrs . item ( i ) ; obj [ attr . nodeName ] = attr . nodeValue ; } } if ( xml . hasChildNodes ( ) ) { const children = [ ] ; const nodes = xml . childNodes ; for ( let i = 0 , len = nodes . length ; i < len ; i ++ ) { const node = nodes . item ( i ) ; const nodeName = node . nodeName ; if ( nodeName === 'parsererror' ) { throw new Error ( node . textContent ) ; } const nodeValue = xmlToJson ( node ) ; if ( nodeValue === null ) { continue ; } children . push ( nodeValue ) ; if ( typeof obj [ nodeName ] === 'undefined' ) { obj [ nodeName ] = nodeValue ; } else { if ( ! Array . isArray ( obj [ nodeName ] ) ) { obj [ nodeName ] = [ obj [ nodeName ] ] ; } obj [ nodeName ] . push ( nodeValue ) ; } } if ( children . length === 1 && obj [ '#text' ] && Object . keys ( obj ) . length === 1 ) { return obj [ '#text' ] ; } } return obj ; }