How To Do Relative xPath In CustomXMLNode?

johnywhy

New Member
Joined
Sep 12, 2008
Messages
47
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Platform
  1. Windows
  2. MacOS
howdy

this code, which loads a KML file, is working great. I'm getting the coordinates of the current oCustomNode:
Code:
Sub Add_Traverse_CustomXMLPart()
    Dim oCustomPart As CustomXMLPart
    Dim oCustomNode As CustomXMLNode
    Dim oCustomNodes As CustomXMLNodes
    
    'Add a Custom XML Part from a file and then load
    Set oCustomPart = ActiveWorkbook.CustomXMLParts.Add
    oCustomPart.Load "C:\Documents and Settings\john\My Documents\Mapping\kml2static\Faith.kml"
    
    'Get Placemarks
    Set oCustomNodes = oCustomPart.SelectNodes("/ns0:kml[1]/ns0:Document[1]/ns0:Placemark")
    For Each oCustomNode In oCustomNodes
        Debug.Print oCustomNode.ChildNodes(8).Text
    Next
    ....
but, would prefer to use relative xPaths, instead of index numbers.

how?

this does not work:
Debug.Print oCustomNode.ChildNodes("Point/coordinates").Text
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
resolved.... I got around this by using the DOM method, instead of CustomXMLPart's.

CustomXMLPart's need namespace and other xPath weirdness that i don't get, and CustomXMLPart's appear to be tangled up in the Excel workbook structure. The DOM method does not involve Excel at all, so this can be ported to .NET.
Code:
    ...
    Dim oDom As New DOMDocument, oNodes As IXMLDOMNodeList, oNode As IXMLDOMNode
    oDom.async = False
    oDom.Load (sKmlUrl)
    Set oNodes = oDom.SelectNodes("//Style/IconStyle/Icon/href")
    For Each oNode In oNodes
    ...
This allows me to use simple xPath expressions as above. I can use oNode.SelectNodes to get child nodes relative to oNode using xPath...
Code:
Set ChildNode = oNode.SelectNodes("href")
CustomXMLPart's also have a SelectNodes method on the CustomXMLNode object, which is the answer to my original question. But i prefer the DOM method for reasons mentioned above.

Requires reference to MSXML (Microsoft XML). sKmlUrl can be a local filepath or a url.
 
Upvote 0

Forum statistics

Threads
1,215,415
Messages
6,124,764
Members
449,187
Latest member
hermansoa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top