Parsing XML with namespace in google apps script
17:00 10 Feb 2018

I'm trying to parse XML from SportsRadar api and retrieve info about medals per country for the Olympics. The API seems to be using namespaces and I can't quite figure out how to parse the XML.

If I look at the top of the XML it looks like this:



  
    
    
    

And if I get the root element and look at the children, I use this code:

var xml_root = xmlData.getRootElement()
Logger.log( xml_root.getChildren() )

and get this output:

[18-02-10 15:29:52:056 EST] [[Element: ], [Element: ]]

So looking at the docs it seems like getChildText(name, namespace) should be my magic ticket to get the medals by country info. So I try:

var tst = xml_root.getChildText('medals_by_country', 'http://schemas.sportradar.com/sportsapi/v1/winterolympics');

but I get an error telling me "Cannot find method getChildText(string,string)". I'm parsing that to mean that the namespace should not be passed as a string, maybe? But I can't figure out what it should be passed as.

I tried just grabbing the first country info by doing:

xml_root.getChild('medals_by_country').getChildren('medal_info');

And xml_root.getChild('medals_by_country') returns null. I presume this is because I'm not referencing it by namespace...

So what should I be doing in order to get the medal info for each country? It feels like I'm close but there's something I'm not groking here.

EDIT: I figured out how to create a namespace (I think). I did the following

var ns = XmlService.getNamespace("xsi", "http://schemas.sportradar.com/sportsapi/v1/winterolympics");

Buuut... I'm still pulling nulls from the xml, much to my chagrin.

xml google-apps-script