?
Solved

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

Posted on 2005-05-02
5
Medium Priority
?
250 Views
Last Modified: 2010-05-02
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>


0
Comment
Question by:seckel
  • 3
5 Comments
 
LVL 12

Expert Comment

by:Preece
ID: 13911636
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
 
LVL 12

Expert Comment

by:Preece
ID: 13912237
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
 

Author Comment

by:seckel
ID: 13916956
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
 
LVL 12

Accepted Solution

by:
Preece earned 2000 total points
ID: 13920189
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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

850 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