Skip to content

Instantly share code, notes, and snippets.

@Sinetheta
Created March 12, 2012 16:20
Show Gist options
  • Save Sinetheta/2023118 to your computer and use it in GitHub Desktop.
Save Sinetheta/2023118 to your computer and use it in GitHub Desktop.
JS: parse XML
// ++ Condense XML
//----------------------------------------------
String.prototype.trimXML = function () {
return this.replace(/>[\s]*</g, "><");
};
// ++ Return the content of a tag
//----------------------------------------------
String.prototype.getTag = function (tag) {
var array = this.match(new RegExp('\\<' + tag + '\\>(.*?)(?=\\<\\/' + tag + '\\>)'));
return array ? array[1] : array;
};
// ++ Return an array the content of tags
//----------------------------------------------
String.prototype.getTags = function (tag) {
var array = this.match(new RegExp('\\<' + tag + '\\>(.*?)(?=\\<\\/' + tag + '\\>)', 'g')),
num;
num = array.length || 0;
while (num) {
array[num - 1] = array[num - 1].replace(new RegExp('<' + tag + '>'), "");
num--;
}
return array;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment