[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Insert a child node into a XML document

Posted on 2001-09-12
3
Medium Priority
?
429 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

872 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