Solved

Insert a child node into a XML document

Posted on 2001-09-12
3
410 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

11 Experts available now in Live!

Get 1:1 Help Now