PDA

View Full Version : How To Do Relative xPath In CustomXMLNode?



johnywhy
01-02-2011, 08:15 PM
howdy

this code, which loads a KML file, is working great. I'm getting the coordinates of the current oCustomNode:
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

Thanks.

johnywhy
01-03-2011, 09:33 PM
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.

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...
Set ChildNodes = 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.