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

Visual Basic ClassicProgramming Languages-Other

Avatar of undefined
Last Comment
vrajvilas

8/22/2022 - Mon
needtoken

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

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

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
vrajvilas

ASKER
Please aslo let me know how will i loop throught all the node in xpath
needtoken

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
needtoken

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
needtoken

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
vrajvilas

ASKER
i get the error at this point

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

do i have to add any library
needtoken

are u using vb6
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
vrajvilas

ASKER
vba
vrajvilas

ASKER
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
needtoken

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
vrajvilas

ASKER
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
vrajvilas

ASKER
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")
vrajvilas

ASKER
excellent answer