Link to home
Start Free TrialLog in
Avatar of vrajvilas
vrajvilas

asked on

VBA - XML how to remove node and copy a node from other xml file and save

Hi all,
  i am new to xml parsing in vba, attached is the xml where i have to remove a node called <Symbology> </Symbology> where the feature name is (<feature name="O_Signal_Structure">)
O_Signal_Structure and copy the node <Symbology> </Symbology>  for the same feature name from another xml file which has the same structrue as the one attached and save the xml file.

i could only get this far and had problem removing the node, please see code below for reference

Public oXMLDocument As DOMDocument60
Public oXMLNode As IXMLDOMNode
Public oXMLElement As IXMLDOMElement
Public oXMLNodeList As IXMLDOMNodeList

Public oXMLNodesData As IXMLDOMElement
Public oXMLSubElement As IXMLDOMElement
Public oXMLSubChildElement As IXMLDOMElement
Public oXMLSubChildElement1 As IXMLDOMElement

Public Sub testXml()

Set oXMLDocument = New DOMDocument60
    oXMLDocument.async = False
    oXMLDocument.Load ("C:\Dev\BMAP Data\depot_own.xml")

    Set oXMLNodesData = oXMLDocument.documentElement()
    Set oXMLNodeList = oXMLNodesData.selectNodes("Workspace")
    For Each oXMLElement In oXMLNodeList
       MsgBox oXMLElement.nodeName
       MsgBox oXMLElement.Text  
        For Each oXMLSubElement In oXMLElement.childNodes
            MsgBox oXMLSubElement.nodeName  
            MsgBox oXMLSubElement.Text              
          For Each oXMLSubChildElement In oXMLSubElement.childNodes
             MsgBox oXMLSubChildElement.nodeName  
             MsgBox oXMLSubChildElement.Text            
             For Each oXMLSubChildElement1 In oXMLSubChildElement.childNodes
                MsgBox oXMLSubChildElement1.nodeName  
                MsgBox oXMLSubChildElement1.Text
                If oXMLSubChildElement1.nodeName = "Symbology" Then
                --------- how to remove the node symbology and insert the symbology node from another xml file--------------------------------------------------------------------------
               
                End If
               
             Next
        Next
        Next
    Next



End Sub

Appreciate your response

Thanks
Vraj




<?xml version="1.0" encoding="utf-16"?>
<GeospatialSchema projectName="test">
 <Workspace name="All Users">
   <Features genFile="features.xml">
      <feature name="O_Signal_Structure" alias="O Signal Structure">
	<Symbology type="cell">
          <ApplyDrawingScale>false</ApplyDrawingScale>
          <OverrideScale />
          <Color>2</Color>
          <Level>O_Signal_Structure</Level>
          <Style>0</Style>
          <StyleScale>1.0</StyleScale>
          <OverrideCellSymbology>true</OverrideCellSymbology>
        </Symbology>
	 <Properties>
          <property name="Accuracy_C" alias="Accuracy C" >
            <initialValue key="placing" synch="true" valueType="value" />
            <dataSpec numChars="5" min="" minExclusive="false" max="" maxExclusive="false" />			  </property>
	</Properties>
	</feature>
	<feature name="O_Signal_Structure_Line" alias="O Signal Structure Line">
	<Symbology type="linear">
          <ApplyDrawingScale>true</ApplyDrawingScale>
          <OverrideScale />
          <Color red="-1" green="-1" blue="-1">20</Color>
          <Level>O_Signal_Structure_Line</Level>
        </Symbology>
		 <Properties>
          <property name="Accuracy_C" alias="Accuracy C" >
            <initialValue key="placing" synch="true" valueType="value" />
            <dataSpec numChars="5" min="" minExclusive="false" max="" maxExclusive="false" />			  </property>
	</Properties>
	</feature>
   </Features>
 </Workspace>
</GeospatialSchema>

Open in new window

Avatar of needtoken
needtoken
Flag of India image

you  please go through the xpath query it is much easier to locate a node.
then delete that selected node
please try this code

it is simple
Dim parent As Xml.XmlNode
        Dim parent1 As Xml.XmlNode

        parent = oXMLDocument .SelectSingleNode("//feature[@name='O_Signal_Structure']")

        parent1 = parent.SelectSingleNode("Symbology")
        parent.RemoveChild(parent1)

Open in new window

Avatar of vrajvilas
vrajvilas

ASKER

do i have to reference as i get the error "user-defined type not defined" and how will i add the node to the same position from another xml file after removing
Please aslo let me know how will i loop throught all the node in xpath
hai vr ajvilas

error may occur due to u r attaching a node from other document

please recreate the node using create node with same document and replace the child
i am attaching the code
' loading document
        domDoc.Load("doc1.xml")
        domDoc1.Load("doc2.xml")

        ' for replacing (doc2)
        Dim parentx As Xml.XmlNode

        ' for doc1 parsing
        Dim parent As Xml.XmlNode
        Dim parent1 As Xml.XmlNode
        Dim parent2 As Xml.XmlNode


        ' node from doc2 to replace
        parentx = domDoc1.SelectSingleNode("//feature[@name='O_Signal_Structure']/Symbology")


        'parent of to be replaced node
        parent = domDoc.SelectSingleNode("//feature[@name='O_Signal_Structure']")

        'current node that is going to replace
        parent1 = parent.SelectSingleNode("Symbology")


        'create a similar node with same document
        parent2 = domDoc.CreateNode(Xml.XmlNodeType.Element, parentx.Name, "")
        'adding its inner xml
        parentx1.InnerXml = parentx.InnerXml


        'replacing it simply
        parent.ReplaceChild(parent2, parent1)

Open in new window

if u want to traverse through a set of nodes that satisfy same xpath string then please use other function
"SelectNodes"  that will return a set of nodes .

traverse with each item
i get the error at this point

Dim parent As Xml.XmlNode
 "user-defined type not defined"

do i have to add any library
are u using vb6
vba
iam using vba so if you can point me which libarary to add then i can add see if the solution works.
ASKER CERTIFIED SOLUTION
Avatar of needtoken
needtoken
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry i could not get back on friday as i had to relocate, i had run the code and found the program does not replace, i dont know if we have to save after replace, below is a little modified version of your program as it erroring on " domDoc.Load" object required.

Public domdoc As DOMDocument60
Public domdoc1 As DOMDocument60

    Set domdoc = New DOMDocument60
    domdoc.async = False
    domdoc.Load ("C:\Dev\BMAP Data\depot_own.xml")


    Set domdoc1 = New DOMDocument60
    domdoc1.async = False
    domdoc1.Load ("C:\Dev\BMAP Data\sig_tool.xml")
   
'      domdoc.Load ("C:\Dev\BMAP Data\depot_own.xml")
'        domdoc1.Load ("C:\Dev\BMAP Data\sig_tool.xml")
'
 
        ' for replacing (doc2)
        Dim parentx As IXMLDOMNode
 
        ' for doc1 parsing
        Dim parent As IXMLDOMNode
        Dim parent1 As IXMLDOMNode
        Dim parent2 As IXMLDOMNode
 
 
        ' node from doc2 to replace
        Set parentx = domdoc1.selectSingleNode("//feature[@name='O_Signal_Structure']/Symbology")
 
 
        'parent of to be replaced node
        Set parent = domdoc.selectSingleNode("//feature[@name='O_Signal_Structure']")
 
        'current node that is going to replace
        Set parent1 = parent.selectSingleNode("Symbology")
 
         
        'replacing it simply
        Call parent.replaceChild(parentx, parent1)
-------------- after this do we have to save to notice the changes
Your response is highly appreciated

i tried adding the below line and the code worked. thanks and i will give you the points.
domdoc.Save ("C:\Dev\BMAP Data\depot_own_1.xml")
excellent answer