I'm having trouble getting to the child elements to the <result> tag.
The code:
var xml = '\
<document>\
<currentTime>2013-09-05 09:47:06</currentTime>\
<result>\
<one>2013-09-05 09:47:06</one>\
<two>2013-09-20 14:30:13</two>\
<three>2013-09-02 14:12:22</three>\
<four>2505</four>\
</result>\
<cachedUntil>2013-09-05 10:28:40</cachedUntil>\
</document>';
var document = XmlService.parse(xml);
var entries = document.getRootElement().getChildren();
Logger.log(entries.length);
for (var i = 0; i < entries.length; i++) {
Logger.log("%s -> %s",entries[i].getName(),entries[i].getText());
}
Running this code returns the following in the logger as I expected:
[13-09-05 13:54:18:815 EAT] 3.0
[13-09-05 13:54:18:815 EAT] currentTime -> 2013-09-05 09:47:06
[13-09-05 13:54:18:816 EAT] result ->
[13-09-05 13:54:18:816 EAT] cachedUntil -> 2013-09-05 10:28:40
I get 3 elements and I'm able to run the getName() and getText() methods just fine. However, if I try to get the children of a specific element like <result> with the line var results = entries.getChildren(); right after I define entries, I get the runtime error "TypeError: Cannot find function getChildren in object [Element: ]". What the deuce?
I don't get what's going on here (obviously). getRootElement() returns an Element type. getChildren() returns an array of Elements. Where is entries getting turned into something that isn't an Element and is there a better way to parse this document? I feel like I'm missing something really stupid here.