O-MI-Node/html/example/example.js
/**
 * Formats XML string, giving it the necessary indentations and line breaks
 * @param {string} xml The XML string
 * @returns {string} The formatted XML string
 */
Formats XML string, giving it the necessary indentations and line breaks
@param {string} xml The XML string
@returns {string} The formatted XML string
Formats XML string giving it the necessary indentations and line breaks @param { string } xml The XML string @returns { string } The formatted XML string
function formatXML(xml) {
        var formatted = '';
    var reg = /(>)(<)(\/*)/g;
    xml = xml.replace(reg, '$1\r\n$2$3');
    var pad = 0;
    jQuery.each(xml.split('\n'), function(index, node) {
        var indent = 0;
                var trim = node.trim();
                if(trim.length > 0){
                        if (trim.match( /.+<\/\w[^>]*>$/ )) {
                                indent = 0;
                        } else if (trim.match( /^<\/\w/ )) {
                                if (pad !== 0) {
                                        pad -= 2;
                                }
                        } else if (trim.match( /^<\w[^>]*[^\/]>.*$/ )) {
                                indent = 2;
                        } else {
                                indent = 0;
                        }
                        var padding = '';
                        for (var i = 0; i < pad; i++) {
                                padding += ' ';
                        }
                        formatted += padding + trim + '\r\n';
                        pad += indent;
                }
    });
    return formatted;
}
function formatXML ( xml ) { var formatted = '' ; var reg = / (>)(<)(\/*) / g ; xml = xml . replace ( reg , '$1\r\n$2$3' ) ; var pad = 0 ; jQuery . each ( xml . split ( '\n' ) , function ( index , node ) { var indent = 0 ; var trim = node . trim ( ) ; if ( trim . length > 0 ) { if ( trim . match ( / .+<\/\w[^>]*>$ / ) ) { indent = 0 ; } else if ( trim . match ( / ^<\/\w / ) ) { if ( pad !== 0 ) { pad -= 2 ; } } else if ( trim . match ( / ^<\w[^>]*[^\/]>.*$ / ) ) { indent = 2 ; } else { indent = 0 ; } var padding = '' ; for ( var i = 0 ; i < pad ; i ++ ) { padding += ' ' ; } formatted += padding + trim + '\r\n' ; pad += indent ; } } ) ; return formatted ; }
Formats XML string, giving it the necessary indentations and line breaks
 @param {string} xml The XML string
 @returns {string} The formatted XML string
Formats XML string giving it the necessary indentations and line breaks @param { string } xml The XML string @returns { string } The formatted XML string