Search code examples
xmlxpathvbscriptasp-classickml

Detect presence of the node and then use attribute and text


Everything in my code is working except for printing the text value of the node. Here is my KML file :

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="AUS_POSTCODE_POLYGON_2015_05" id="AUS_POSTCODE_POLYGON_2015_05">
</Schema>
<Folder><name>AUS_POSTCODE_POLYGON_2015_05</name>
  <Placemark>
  <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
  <ExtendedData><SchemaData schemaUrl="#AUS_POSTCODE_POLYGON_2015_05">
    <SimpleData name="DT_CREATE">20150423</SimpleData>
    <SimpleData name="POSTCODE">5700</SimpleData>
    <SimpleData name="STATE_PID">4</SimpleData>
    <SimpleData name="STATE">SA</SimpleData>
  </SchemaData></ExtendedData>
    <MultiGeometry><Polygon><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode>
    <coordinates>137.745052224211719,-32.403930130629746 </coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
  </Placemark>
  <Placemark>
  <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
  <ExtendedData><SchemaData schemaUrl="#AUS_POSTCODE_POLYGON_2015_05">
    <SimpleData name="DT_CREATE">20150423</SimpleData>
  </SchemaData></ExtendedData>
    <MultiGeometry><Polygon><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode>
    <coordinates>139.971166832040183,-32.421544630638806</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
  </Placemark>
  <Placemark>
  <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
  <ExtendedData><SchemaData schemaUrl="#AUS_POSTCODE_POLYGON_2015_05">
    <SimpleData name="DT_CREATE">20141021</SimpleData>
    <SimpleData name="POSTCODE">5022</SimpleData>
    <SimpleData name="STATE_PID">4</SimpleData>
    <SimpleData name="STATE">SA</SimpleData>
  </SchemaData></ExtendedData>
    <MultiGeometry><Polygon><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode>
    <coordinates>138.485436120358145,-34.875330494997144</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
  </Placemark>
</Folder>
</Document></kml>

and here is my VBScript:

Dim fso, fPath , i, xpath, placemark, simpleDataNode, stateID, postCode
dim objxml
Set fso  = CreateObject("Scripting.FileSystemObject")
fPath    = fso.GetAbsolutePathName(fromFileName)
Set objxml = CreateObject("Msxml2.DOMDocument")
objxml.SetProperty "SelectionNamespaces", "xmlns:base=""http://www.opengis.net/kml/2.2"""
objXML.async =  False
objXML.Load fPath

If 0 = objXML.ParseError Then
  xpath     = "//base:Placemark"
  Set placemark  = objXML.selectNodes(xpath)
  writeLog  placemark.length & " = how many nodes found"
  If 0 < placemark.length Then
    For Each simpleDataNode In placemark 
      Set stateID  = simpleDataNode.selectSingleNode("ExtendedData/SchemaData/SimpleData[@name='STATE_PID']")
      Set postCode = simpleDataNode.selectSingleNode("ExtendedData/SchemaData/SimpleData[@name='POSTCODE']")
      If Not stateID Is Nothing Then
        writeLog stateID.getAttribute("name")
        'writeLog stateID.item(0).Text     < gives me error
      else
        writeLog "no state node found "
      end if
      If Not postCode Is Nothing Then
        writeLog postCode.getAttribute("name")
        'writeLog postCode.item(0).Text    < gives me error
      else
        writeLog "no postCode node node found "
      end if
    Next
  Else
    writeLog "not found |" & xpath & "|"
  End If
Else
  writeLog objXML.ParseError.Reason
End If

The code goes through each Placemark node and detects:

  • is there is a SimpleData node with a STATE_PID value? if so print the the name and the node text
  • is there is a SimpleData node with a POSTCODE value? if so print the the name and the node text

This line for both questions works:

writeLog stateID.getAttribute("name")

But this line does not:

writeLog stateID.item(0).Text     < gives me error

It give me a object doesn't support this property or method. How can I print the nodes text value?

While going through this Placemark node I would also like to print the Placemark text within the coordinates node.

Here is the output. As you can see it correctly tells me the second Placemark node is missing the state and postcode nodes:

3 = how many nodes found
STATE_PID
POSTCODE
no state node found 
no postCode node node found   
STATE_PID
POSTCODE

Solution

  • stateID and postCode are simple (text) nodes, not collections. So use stateID.text and postCode.text.