Solved

Insert a child node into a XML document

Posted on 2001-09-12
3
413 Views
Last Modified: 2012-08-13
I have a XML document with formated:

<MyList>
 <MyGroup>
  <GroupID>1</GroupID>
  <GroupName>Name 1</GroupName>
 </MyGroup>
 <MyGroup>
  <GroupID>1</GroupID>
  <GroupName>Name 2</GroupName>
 </MyGroup>
 <MyGroup>
  <GroupID>2</GroupID>
  <GroupName>Name 4</GroupName>
 </MyGroup>
</MyList>

Suppose I have a new node(GroupID = 1, GroupName = Name 5). How could I insert the new node into the XML doc, at the end of nodes have GroupID = 1 (and ofcourse, before the node has GroupID = 2)

Thanks in advance
nguyenn
0
Comment
Question by:nguyenn
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 100 total points
ID: 6477337
im xmldoc As MSXML2.DOMDocument

' Load the XML document
Set xmldoc = New MSXML2.DOMDocument
xmldoc.Load "c:\temp\temp.xml"

AddNode xmldoc, "1", "Name 5"
Debug.Print xmldoc.xml

AddNode xmldoc, "3", "Name 6"
Debug.Print xmldoc.xml

Set xmldoc = Nothing

End Sub

Sub AddNode(xmldoc As MSXML2.DOMDocument, ByVal GroupID As String, ByVal GroupName As String)
Dim xmlNewNode As MSXML2.IXMLDOMNode
Dim xmlNode As MSXML2.IXMLDOMNode
Dim Done As Boolean

' Create the node
Set xmlNewNode = xmldoc.createNode(NODE_ELEMENT, "MyGroup", vbNullString)
Set xmlNode = xmlNewNode.appendChild(xmldoc.createNode(NODE_ELEMENT, "GroupID", vbNullString))
xmlNode.Text = GroupID
Set xmlNode = xmlNewNode.appendChild(xmldoc.createNode(NODE_ELEMENT, "GroupName", vbNullString))
xmlNode.Text = GroupName

' Find the node to insert by looping through all the MyGroup siblings
' Note this could probably be improved by searching for the element that has GroupID + 1
Set xmlNode = xmldoc.selectSingleNode("/MyList/MyGroup")
Done = False
Do While Not xmlNode Is Nothing And Not Done
   If CLng(xmlNode.childNodes(0).Text) > CLng(GroupID) Then
      Done = True
   Else
      Set xmlNode = xmlNode.nextSibling
   End If
Loop
xmldoc.documentElement.insertBefore xmlNewNode, xmlNode

End Sub
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6477339
Let's try again:

Private Sub Command1_Click()
Dim xmldoc As MSXML2.DOMDocument

' Load the XML document
Set xmldoc = New MSXML2.DOMDocument
xmldoc.Load "c:\temp\temp.xml"

AddNode xmldoc, "1", "Name 5"
Debug.Print xmldoc.xml

AddNode xmldoc, "3", "Name 6"
Debug.Print xmldoc.xml

Set xmldoc = Nothing

End Sub

Sub AddNode(xmldoc As MSXML2.DOMDocument, ByVal GroupID As String, ByVal GroupName As String)
Dim xmlNewNode As MSXML2.IXMLDOMNode
Dim xmlNode As MSXML2.IXMLDOMNode
Dim Done As Boolean

' Create the node
Set xmlNewNode = xmldoc.createNode(NODE_ELEMENT, "MyGroup", vbNullString)
Set xmlNode = xmlNewNode.appendChild(xmldoc.createNode(NODE_ELEMENT, "GroupID", vbNullString))
xmlNode.Text = GroupID
Set xmlNode = xmlNewNode.appendChild(xmldoc.createNode(NODE_ELEMENT, "GroupName", vbNullString))
xmlNode.Text = GroupName

' Find the node to insert
Set xmlNode = xmldoc.selectSingleNode("/MyList/MyGroup")
Done = False
Do While Not xmlNode Is Nothing And Not Done
   If CLng(xmlNode.childNodes(0).Text) > CLng(GroupID) Then
      Done = True
   Else
      Set xmlNode = xmlNode.nextSibling
   End If
Loop
xmldoc.documentElement.insertBefore xmlNewNode, xmlNode

End Sub
0
 
LVL 1

Author Comment

by:nguyenn
ID: 6477413
Thanks for your excellent codes, Acperkins. You help me solve out the problem

I very appreciate your help, and have a nice day :)
nguyenn
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now