Skip to content

Instantly share code, notes, and snippets.

@AinTunez
Last active September 30, 2016 17:13
Show Gist options
  • Select an option

  • Save AinTunez/5c01f40aa4ee262d64401d9275b182bd to your computer and use it in GitHub Desktop.

Select an option

Save AinTunez/5c01f40aa4ee262d64401d9275b182bd to your computer and use it in GitHub Desktop.
XML String Formatter
function formatXML(input) {
// PART 1: Add \n where necessary
// A) add \n between sets of angled brackets without content between them
// B) remove \n between opening and closing tags of the same node if no content is between them
// C) add \n between a self-closing set of angled brackets and the next set
// D) split it into an array
xmlString = input.trim()
.replace(/>\s*</g,'>\n<')
.replace(/(<[^\/>].*>)\n(<[\/])/g,'$1$2')
.replace(/(<\/[^>]+>|<[^>]+\/>)(<[^>]+>)/g,'$1\n$2');
xmlArr = xmlString.split('\n');
// PART 2: indent each line appropriately
var tabs = ''; //store the current indentation
var start = 0; //starting line
if (/^<[?]xml/.test(xmlArr[0])) start++; //if the first line is a header, ignore it
for (var i = start; i < xmlArr.length; i++) { //for each line
var line = xmlArr[i].trim(); //trim it just in case
if (/^<[/]/.test(line)) { // if the line is a closing tag
// remove one tab from the store
// add the tabs at the beginning of the line
tabs = tabs.replace(/.$/, '');
xmlArr[i] = tabs + line;
} else if (/<.*>.*<\/.*>|<.*[^>]\/>/.test(line)) { // if the line contains an entire node
// leave the store as is
// add the tabs at the beginning of the line
xmlArr[i] = tabs + line;
} else { // if the line starts with an opening tag and does not contain an entire node
// add the tabs at the beginning of the line
// and add one tab to the store
xmlArr[i] = tabs + line;
tabs += '\t';
}
}
//rejoin the array to a string and return it
return xmlArr.join('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment