Parse KML file with vba
<p>I am trying to get 2 pieces of information from a KML file. I have some code (ref:K Buttters) in vba (as I want to add the data to a db). This code works perfectly and returns the coordinates, but I want to also get the data for the name tag. How do I amend the code to get this extra piece of information. information. Thanks</p>

<pre><code>Public Sub readFile()

Dim objXML As MSXML2.DOMDocument60

Dim ns As String

Dim nodelist As IXMLDOMNodeList
Dim coordNode As IXMLDOMNode
Dim XPath As String
Dim Namespace As String

Dim i As Integer    

'Create the DomDocument Object

'Set objXML = CreateObject("MSXML2.DOMDocument60")

Set objXML = New MSXML2.DOMDocument60

'Load entire Document before moving on

objXML.async = False

'Don't Validate

objXML.validateOnParse = False

'Read in the file

objXML.Load ("C:\pcsect2.kml")


''''''''''''''''''''''''''''''

' Set up the appropriate (default) namespace

''''''''''''''''''''''''''''''

Namespace = "xmlns:ns='http://www.opengis.net/kml/2.2'"

Call objXML.SetProperty("SelectionNamespaces", Namespace)


Call objXML.SetProperty("SelectionLanguage", "XPath")


''''''''''''''''''''''''''''''

' Define the XPath to get to coordinates

''''''''''''''''''''''''''''''

XPath = "//ns:coordinates"

''''''''''''''''''''''''''''''

' Select All "coordinates" nodes

''''''''''''''''''''''''''''''

Set nodelist = objXML.SelectNodes(XPath)

''''''''''''''''''''''''''''''

' Nodelist contains all the Coordinates...

' note: Under view ... set immediate window to viewable to see the debug.print results...

''''''''''''''''''''''''''''''

For i = 0 To nodelist.Length - 1

    Set coordNode = nodelist.NextNode

    Debug.Print coordNode.Text

Next i

End Sub
</code></pre>

<hr>

<p>kml file:</p>

<pre><code>                &lt;kml xmlns="http://www.opengis.net/kml/2.2"&gt;
              &lt;Document&gt;
                &lt;description&gt;&lt;![CDATA[UK postcode polygons]]&gt;&lt;/description&gt;
                &lt;Style id="stylePostcode"&gt;
                  &lt;LineStyle&gt;
                    &lt;width&gt;3&lt;/width&gt;
                  &lt;/LineStyle&gt;
                  &lt;PolyStyle&gt;
                    &lt;color&gt;00ff0000&lt;/color&gt;
                  &lt;/PolyStyle&gt;
                &lt;/Style&gt;
                &lt;Style id="stylePostcode" xmlns="http://www.opengis.net/kml/2.2"&gt;
                  &lt;LineStyle&gt;
                    &lt;width&gt;3&lt;/width&gt;
                  &lt;/LineStyle&gt;
                  &lt;PolyStyle&gt;
                    &lt;color&gt;00ff0000&lt;/color&gt;
                  &lt;/PolyStyle&gt;
                &lt;/Style&gt;    
                &lt;Folder xmlns="http://www.opengis.net/kml/2.2"&gt;
                  &lt;name&gt;AB10&lt;/name&gt;
                  &lt;description&gt;&lt;![CDATA[Postcode sectors in the AB10 postcode district]]&gt;&lt;/description&gt;
                  &lt;Placemark&gt;
                    &lt;name&gt;AB10 1&lt;/name&gt;
                    &lt;styleUrl&gt;#stylePostcode&lt;/styleUrl&gt;
                    &lt;description&gt;&lt;![CDATA[AB10 1 postcode sector]]&gt;&lt;/description&gt;
                    &lt;MultiGeometry&gt;
                      &lt;Polygon&gt;
                        &lt;outerBoundaryIs&gt;
                          &lt;LinearRing&gt;
                            &lt;coordinates&gt;
            -2.09646046762775,57.1512327554617 -2.0962834594401,57.1506128321186 -2.09610247782767            
                            &lt;/coordinates&gt;
                          &lt;/LinearRing&gt;
                        &lt;/outerBoundaryIs&gt;
                      &lt;/Polygon&gt;
                      &lt;Polygon&gt;
                        &lt;outerBoundaryIs&gt;
                          &lt;LinearRing&gt;
                            &lt;coordinates&gt;
            -2.10596557515499,57.1385921418501 -2.10604057319705,57.1387481221535           
                          &lt;/LinearRing&gt;
                        &lt;/outerBoundaryIs&gt;
                      &lt;/Polygon&gt;
                    &lt;/MultiGeometry&gt;
                  &lt;/Placemark&gt;    
                  &lt;Placemark&gt;
                    &lt;name&gt;AB10 6&lt;/name&gt;
                    &lt;styleUrl&gt;#stylePostcode&lt;/styleUrl&gt;
                    &lt;description&gt;&lt;![CDATA[AB10 6 postcode sector&lt;/a&gt;]]&gt;&lt;/description&gt;
                    &lt;MultiGeometry&gt;
                      &lt;Polygon&gt;
                        &lt;outerBoundaryIs&gt;
                          &lt;LinearRing&gt;
                            &lt;coordinates&gt;
            -2.11311153246704,57.1365722876569 -2.11309853402536,57.1365692881912          
                          &lt;/LinearRing&gt;
                        &lt;/outerBoundaryIs&gt;
                      &lt;/Polygon&gt;
                      &lt;Polygon&gt;
                        &lt;outerBoundaryIs&gt;
                          &lt;LinearRing&gt;
                            &lt;coordinates&gt;
            -2.1113360807897,57.1427795678869 -2.11112010338182,57.1426645845693
                      &lt;/LinearRing&gt;
                        &lt;/outerBoundaryIs&gt;
                      &lt;/Polygon&gt;
                    &lt;/MultiGeometry&gt;
                  &lt;/Placemark&gt;
                  &lt;Placemark&gt;
                    &lt;name&gt;AB10 7&lt;/name&gt;
                    &lt;styleUrl&gt;#stylePostcode&lt;/styleUrl&gt;
                    &lt;description&gt;&lt;![CDATA[AB10 7 postcode sector&gt;Details&lt;/a&gt;]]&gt;&lt;/description&gt;
                    &lt;MultiGeometry&gt;
                      &lt;Polygon&gt;
                        &lt;outerBoundaryIs&gt;
                          &lt;LinearRing&gt;
                            &lt;coordinates&gt;
            -2.11383946866067,57.1372032022769 -2.11366248946505,57.1371542105275         
                          &lt;/LinearRing&gt;
                        &lt;/outerBoundaryIs&gt;
                      &lt;/Polygon&gt;
                    &lt;/MultiGeometry&gt;
                  &lt;/Placemark&gt;
                &lt;/Folder&gt;
                &lt;Style id="stylePostcode" xmlns="http://www.opengis.net/kml/2.2"&gt;
                  &lt;LineStyle&gt;
                    &lt;width&gt;3&lt;/width&gt;
                  &lt;/LineStyle&gt;
                  &lt;PolyStyle&gt;
                    &lt;color&gt;00ff0000&lt;/color&gt;
                  &lt;/PolyStyle&gt;
                &lt;/Style&gt;    
                &lt;Folder xmlns="http://www.opengis.net/kml/2.2"&gt; 
                &lt;name&gt;ZE3&lt;/name&gt;
                  &lt;description&gt;&lt;![CDATA[Postcode sectors in the ZE3 postcode district]]&gt;&lt;/description&gt;
                  &lt;Placemark&gt;
                    &lt;name&gt;ZE3 9&lt;/name&gt;
                    &lt;styleUrl&gt;#stylePostcode&lt;/styleUrl&gt;
                    &lt;description&gt;&lt;![CDATA[ZE3 9 postcode sector&gt;Details&lt;/a&gt;]]&gt;&lt;/description&gt;
                    &lt;MultiGeometry&gt;
                      &lt;Polygon&gt;
                        &lt;outerBoundaryIs&gt;
                          &lt;LinearRing&gt;
                            &lt;coordinates&gt;
            -1.33983120448744,59.8989006252828 -1.32301876923355,59.9016455421112             
                          &lt;/LinearRing&gt;
                        &lt;/outerBoundaryIs&gt;
                      &lt;/Polygon&gt;
                      &lt;Polygon&gt;
                        &lt;outerBoundaryIs&gt;
                          &lt;LinearRing&gt;
                            &lt;coordinates&gt;
            -1.32885903137389,59.8719950412819 -1.33463302343859,59.8690613106313  
                          &lt;/LinearRing&gt;
                        &lt;/outerBoundaryIs&gt;
                      &lt;/Polygon&gt;
                    &lt;/MultiGeometry&gt;
                  &lt;/Placemark&gt;
                &lt;/Folder&gt;
              &lt;/Document&gt;
            &lt;/kml&gt;
</code></pre>