Created
March 12, 2012 16:20
-
-
Save Sinetheta/2023118 to your computer and use it in GitHub Desktop.
JS: parse XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ++ 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