Forked from petersirka/JavaScript - Simple XML parser
Created
October 23, 2024 05:19
-
-
Save andhikayuana/96b4a56cdcc0826faa395496797d32b9 to your computer and use it in GitHub Desktop.
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
/** | |
* Simple XML parser | |
* @param {String} xml | |
* @return {Object} | |
*/ | |
function parseXML(xml) { | |
var beg = -1; | |
var end = 0; | |
var tmp = 0; | |
var current = []; | |
var obj = {}; | |
var from = -1; | |
while (true) { | |
beg = xml.indexOf('<', beg + 1); | |
if (beg === -1) | |
break; | |
end = xml.indexOf('>', beg + 1); | |
if (end === -1) | |
break; | |
var el = xml.substring(beg, end + 1); | |
var c = el[1]; | |
if (c === '?' || c === '/') { | |
var o = current.pop(); | |
if (from === -1 || o !== el.substring(2, el.length - 1)) | |
continue; | |
var path = current.join('.') + '.' + o; | |
var value = xml.substring(from, beg); | |
if (typeof(obj[path]) === 'undefined') | |
obj[path] = value; | |
else if (obj[path] instanceof Array) | |
obj[path].push(value); | |
else | |
obj[path] = [obj[path], value]; | |
from = -1; | |
continue; | |
} | |
tmp = el.indexOf(' '); | |
var hasAttributes = true; | |
if (tmp === -1) { | |
tmp = el.length - 1; | |
hasAttributes = false; | |
} | |
from = beg + el.length; | |
var isSingle = el[el.length - 2] === '/'; | |
var name = el.substring(1, tmp); | |
if (!isSingle) | |
current.push(name); | |
if (!hasAttributes) | |
continue; | |
var match = el.match(/\w+\=\".*?\"/g); | |
if (match === null) | |
continue; | |
var attr = {}; | |
var length = match.length; | |
for (var i = 0; i < length; i++) { | |
var index = match[i].indexOf('"'); | |
attr[match[i].substring(0, index - 1)] = match[i].substring(index + 1, match[i].length - 1); | |
} | |
obj[current.join('.') + (isSingle ? '.' + name : '') + '[]'] = attr; | |
} | |
return obj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment