Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries [Thread Prev] >Thread Next - Re: Modifying XML with VBA Modifying XML with VBATo: 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?
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
