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