How to use MSXML to modify an XML element?

M

matturn

Hi,

I'm trying to use MSXML to modify an XML element without much luck. It
seems to be that I have to delete the node I want to modify, then
create a new one.

My code so far:

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
Dim strNewNode As String

'Load your xml document int 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")

elementStyle = oxmlDoc.createElement("styleURL")
textStyle = oxmlDoc.createTextNode("#" & strName)
elementStyle.appendChild (textStyle)
oxmlNode.appendChild (elementStyle)
Next

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

End Sub


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?
 
S

SvenC

I'm trying to use MSXML to modify an XML element without much luck. It
seems to be that I have to delete the node I want to modify, then
create a new one.

...
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")

Why did you uncomment the above?
Select and modify the nodes text like so:

Set oxmlStyleNode = oxmlNode.SelectSingleNode("//styleUrl")
oxmlStyleNode.Text = "#" & strName
elementStyle = oxmlDoc.createElement("styleURL")
textStyle = oxmlDoc.createTextNode("#"& strName)
elementStyle.appendChild (textStyle)
oxmlNode.appendChild (elementStyle)

You do not need the above 4 lines.
 
M

matturn

Thanks Sven. The function modifies the XML now :)

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?
 
Top