How do I copy and insert nodes into an XML layout?

I need to add a node based on the number of licenses a person may have.  How do I do the following using VB6:

1.  Find the "License" node in my XML.
2.  Copy and insert the the license node (including all child nodes) for each different license.  
     There will always be one license node.

Input XML:
<DriverInfo>
   <Miscellanceous></Miscellanceous>
   <PersonInfo>
      <Name></Name>
      <BirthDate></BirthDate>
  </PersonInfo>
   <License>
     <Number></Number>
     <StateCode><StateCode>  
   </License>
</DriverInfo>

Output XML I want to have for a person with 3 licenses:
<DriverInfo>
   <Miscellanceous></Miscellanceous>
   <PersonInfo>
      <Name></Name>
      <BirthDate></BirthDate>
  </PersonInfo>
   <License>
     <Number></Number>
     <StateCode><StateCode>  
   </License>
   <License>
     <Number></Number>
     <StateCode><StateCode>  
   </License>
   <License>
     <Number></Number>
     <StateCode><StateCode>  
   </License>
</DriverInfo>


seckelAsked:
Who is Participating?
 
PreeceCommented:
With some modification, give this a try:


gsnCopyAndAppendNode webX, App.Path & "\driver.xml", "License"


Public Sub gsnCopyAndAppendNode(webX As WebBrowser, sFileName As String, sWhichNode As String)
    Dim oDoc As MSXML.DOMDocument
    Dim elmRoot As IXMLDOMElement
    Dim elmNew As IXMLDOMElement
    Dim oNodeList As IXMLDOMNodeList
    Dim sNodeName As String
    Dim lX As Long
    '
    ' init our xml doc
    Set oDoc = New MSXML.DOMDocument
    '
    ' load xml file into oDoc
    oDoc.Load sFileName
    '
    ' set root element
    Set elmRoot = oDoc.documentElement
    '
    ' set the nodelist
    Set oNodeList = oDoc.documentElement.childNodes
    '
    ' cycle until you get to the sWhich node
    For lX = 0 To oNodeList.length - 1
        oNodeList.nextNode
        sNodeName = oNodeList.Item(lX).nodeName
        ' Look for match on node name
        If sNodeName = sWhichNode Then
            ' create new node
            Set elmNew = oNodeList.Item(lX)
            ' clone and append child
            oDoc.documentElement.appendChild elmNew.cloneNode(True)
            Exit For
        End If
    Next lX
    '
    ' Save doc to xml file
    oDoc.save sFileName
    webX.Navigate sFileName
    '
    ' destroy objects
    Set oNodeList = Nothing
    Set elmNew = Nothing
    Set elmRoot = Nothing
    Set oDoc = Nothing

End Sub


The above code will copy and append the first instance of the named node, "License", along with all of it's child elements and data...

Preece
0
 
PreeceCommented:
Assuming that you already have a "license.xml" in the project folder, check this out:

gsnAddLicenseInfo webX, App.Path & "\license.xml"

Public Sub gsnAddLicenseInfo(webX As WebBrowser, sFileName As String)
    Dim oDoc As MSXML.DOMDocument
    Dim elmRoot As IXMLDOMElement
    Dim elmNew As IXMLDOMElement
    Dim lX As Long
    '
    ' init our xml doc
    Set oDoc = New MSXML.DOMDocument
    '
    ' load xml file into oDoc
    oDoc.Load sFileName
    '
    ' set root element
    Set elmRoot = oDoc.documentElement
    '
    ' create and return a new element
    Set elmNew = gfCreateReturnElement(oDoc, elmRoot, "License", "ElementValueHere")
    '
    ' add child elements to the new element
    gsnCreateElementWithValue oDoc, elmNew, "Number", "ElementValueHere"
    gsnCreateElementWithValue oDoc, elmNew, "StateCode", "ElementValueHere"
    '
    ' Save doc to xml file
    oDoc.save App.Path & "\license.xml"
    webX.Navigate App.Path & "\license.xml"
    '
    ' destroy objects
    Set elmNew = Nothing
    Set elmRoot = Nothing
    Set oDoc = Nothing

End Sub
0
 
PreeceCommented:
Oh, yeah, you'll need this sub as well!  Sorry!


Public Sub gsnCreateElementWithValue(oDoc As MSXML.DOMDocument, elmParent As IXMLDOMElement, sElementName As String, sElementValue As String)
    Dim oChild As IXMLDOMElement
    Dim oTextNode As IXMLDOMText
   
    Set oChild = oDoc.createElement(sElementName)
    Set oTextNode = oDoc.createTextNode(sElementValue)
    oChild.appendChild oTextNode
    elmParent.appendChild oChild
    Set oChild = Nothing
    Set oTextNode = Nothing
End Sub


Preece
0
 
seckelAuthor Commented:
The license node has many child nodes and sub-child nodes.   The example I gave is for simplicity.  Is there a way to copy the enter node and all child nodes without having to explicitly define each one?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.