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--------------------------------------------------------------------------
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)
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)
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.
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.
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")
' 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
then delete that selected node