Last active
October 14, 2015 15:40
-
-
Save loadster/12ed8bc187534d182ba0 to your computer and use it in GitHub Desktop.
Parse and capture a value from an XML document by iterating elements
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
// <users> | |
// <user id="1"><fullName>Hank Hill</fullName></user> | |
// <user id="2"><fullName>Peggy Hill</fullName></user> | |
// <user id="3"><fullName>Bobby Hill</fullName></user> | |
// </users> | |
function capture(response) { | |
var xml = XML.parse(response.bodyAsString); | |
var users = xml.childrenNamed("user"); | |
for (var i = 0; i < users.length; i++) { | |
var user = users[i]; | |
if (user.childNamed('fullName').val == "Bobby Hill") { | |
return user.attr['id']; // That's him! | |
} | |
} | |
return ""; // Bobby Hill not found! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment