Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Modifying XML with VBA

From: matturn <matturn@-----.--->
To: NULL
Date: 1/22/2009 2:23:00 AM
Hi,

I'm trying to modify some XML with VBA, something I haven't done
before. Thanks to the help of microsoft.public.office.developer.vba,
I've come closer to succeeding.

Unfortunately, there's something wrong with the way I'm trying to
select nodes within the nodelist. I'm trying to select all the nodes
with a certain "styleURL" value, put them in a nodelist, then process
each individual node. However, when I try to find the "name" value and
set the "styleURL" value of these selected nodes, the code appears to
refer to the entire DOMDocument. It cycles through once for each of
the selected nodes, and sets the first "styleURL" value in the
imported XML with the first "name" value in the XML.

How can I get the xpath query to only reference the selected node and
it's children?


Sub ChangeNode()

    Dim oxmlDoc As MSXML2.DOMDocument
    Dim oxmlNameNode As MSXML2.IXMLDOMNode
    Dim oxmlNode As MSXML2.IXMLDOMNode
    Dim oxmlNodes As MSXML2.IXMLDOMNodeList
    Dim strName As String

    'Load xml document into a dom document
    Set oxmlDoc = New DOMDocument
    oxmlDoc.async = False
    oxmlDoc.Load (ThisWorkbook.Path & "\data.kml")

    'Find and select all the Placemarks using the #trb248 style
    Set oxmlNodes = oxmlDoc.SelectNodes("//Placemark
[styleUrl='#trb248']")

    'Change the selected styles
    For Each oxmlNode In oxmlNodes
        Set oxmlNameNode = oxmlNode.SelectSingleNode("//name")
        strName = Replace(Mid(oxmlNameNode.XML, 7, Len
(oxmlNameNode.XML) - 13), " ", "")
        Set oxmlStyleNode = oxmlNode.SelectSingleNode("//styleUrl")
        oxmlStyleNode.Text = "#" & strName
    Next

    'Save styles.kml
    oxmlDoc.Save ThisWorkbook.Path & "\data.kml"

End Sub


Extremely truncated sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<kml>
  <Document>
<Placemark id="235">
      <Snippet maxLines="0">
      </Snippet>
      <name>Boroondara (C) - Camberwell N.</name>
      <description>
      </description>
      <visibility>1</visibility>
      <open>0</open>
      <Polygon>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>clampedToGround</altitudeMode>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates>145.10388096,-37.7965841440861,0
145.10406464,-37.7963139330861,0 </coordinates>
          </LinearRing>
        </outerBoundaryIs>
      </Polygon>
      <styleUrl>#trb248</styleUrl>
    </Placemark>
</Document>
</kml>

Any ideas? Should I be using a different approach, say using regular
expressions?


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent