Recommended syntax/semantics for parsing XML

I’m using this code to parse coordinates from a KML (xml) file. Is there a more straight forward way drill into a data with deeply nested arrays of maps of arrays?

I’m more familiar with XPath or LInq, for example, But I’ve read these are not supported in Go or Hugo.

Snippet

{{ $folderName := "Places1" }}
{{ $placeName := "home" }}
{{ $folder := index (where .Site.Data.test.Document.Folder "name" $folderName) 0 }}
{{ debug.Dump ( index ( where $folder.Placemark "name" $placeName) 0 ).Point.coordinates}}

Sample KML

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
	<Folder>
    asdsad
		<name>Places1</name>
		<open>1</open>
		<Placemark>
			<name>home</name>
			<LookAt>
				<longitude>-126.3367438000934</longitude>
				<latitude>50.22476255664978</latitude>
				<altitude>0</altitude>
				<heading>35.42705455875917</heading>
				<tilt>41.41090368844717</tilt>
				<range>1762.066150215274</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-126.3363483039566,50.22617188222286,0</coordinates>
			</Point>
		</Placemark>
		<Placemark>
			<name>work</name>
			<visibility>0</visibility>
			<LookAt>
				<longitude>-125.7237372351031</longitude>
				<latitude>49.8833713008373</latitude>
				<altitude>0</altitude>
				<heading>-66.16030558763055</heading>
				<tilt>25.77323748991006</tilt>
				<range>2453.223795630301</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin4</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-125.7197422049882,49.88759872356041,0</coordinates>
			</Point>
		</Placemark>
	</Folder>
    <Folder>
		<name>Places2</name>
		<open>1</open>
		<Placemark>
			<name>home</name>
			<LookAt>
				<longitude>-126.3367438000934</longitude>
				<latitude>50.22476255664978</latitude>
				<altitude>0</altitude>
				<heading>35.42705455875917</heading>
				<tilt>41.41090368844717</tilt>
				<range>1762.066150215274</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-126.3363483039566,50.22617188222286,0</coordinates>
			</Point>
		</Placemark>
		<Placemark>
			<name>work</name>
			<visibility>0</visibility>
			<LookAt>
				<longitude>-125.7237372351031</longitude>
				<latitude>49.8833713008373</latitude>
				<altitude>0</altitude>
				<heading>-66.16030558763055</heading>
				<tilt>25.77323748991006</tilt>
				<range>2453.223795630301</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin4</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-125.7197422049882,49.88759872356041,0</coordinates>
			</Point>
		</Placemark>
	</Folder>
</Document>
</kml>

No.

You don’t have an object identified by “home”, so you need to use where to find matching objects in the array. And where returns a slice (conceptually an array) so you need to use index to get the first (and only) element in the slice.

The approach would the same reqardless of format: JSON, YAML, TOML, or XML.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.