parseXML.js

var users = []; var userNodes = responseXML.getElementsByTagName('users'); var node, usernameNodes, usernameNode, username, realnameNodes, realnameNode, realname, emailNodes, emailNode, email; for (var i = 0, len = userNodes.length; i < len; i++) { node = userNodes[i]; username = realname = email = ''; usernameNodes = node.getElementsByTagName('username'); if (usernameNodes && usernameNodes[0]) { usernameNode = usernameNodes[0]; username = (usernameNodes.firstChild) ? usernameNodes.firstChild.nodeValue : ''; } realnameNodes = node.getElementsByTagName('realname'); if (realnameNodes && realnameNodes[0]) { realnameNode = realnameNodes[0]; realname = (realnameNodes.firstChild) ? realnameNodes.firstChild.nodeValue : ''; } emailNodes = node.getElementsByTagName('email'); if (emailNodes && emailNodes[0]) { emailNode = emailNodes[0]; email = (emailNodes.firstChild) ? emailNodes.firstChild.nodeValue : ''; } users[i] = { id: node.getAttribute('id'), username: username, realname: realname, email: email }; } return users; }
In general, parsing XML requires a great deal of effort on the part of the JavaScript programmer. Aside from knowing the particulars of the structure ahead of time, you must also know exactly how to pull apart that structure and painstakingly reassemble it into a JavaScript object.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.