Solved

Insert a child node into a XML document

Posted on 2001-09-12
3
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

739 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