xml and vb6: Add nodes at every repeating level

So, here is what I have:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
&#9;<env:Body>
&#9;&#9;<dp:request domain="D" xmlns:dp="http://www.datapower.com/schemas">
&#9;&#9;&#9;<dp:do-action> 
&#9;&#9;&#9;</dp:do-action>
&#9;&#9;</dp:request>
&#9;</env:Body>
</env:Envelope>

Open in new window


Here is what I need to do:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
&#9;<env:Body>
&#9;&#9;<dp:request domain="D" xmlns:dp="http://www.datapower.com/schemas">
&#9;&#9;&#9;<dp:do-action>
&#9;&#9;&#9;&#9;<DeleteFile>
&#9;&#9;&#9;&#9;&#9;<File>logtemp: ///filename1</File>
&#9;&#9;&#9;&#9;</DeleteFile>
&#9;&#9;&#9;&#9;<DeleteFile>
&#9;&#9;&#9;&#9;&#9;<File>logtemp: ///filename2</File>
&#9;&#9;&#9;&#9;</DeleteFile> 
&#9;&#9;&#9;</dp:do-action>
&#9;&#9;</dp:request>
&#9;</env:Body>
</env:Envelope>

Open in new window


I want to create two new nodes - "DeleteFile" and "File" under "dp:request domain" node that repeat for a certain number of times (which is taken care of a For loop though... for now assume 2 times)

How? :)
JammerulesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JammerulesAuthor Commented:
Bump!
JammerulesAuthor Commented:
Wondering if this question is that difficult that nobody made a comment yet!! o_O
andrewssd3Commented:
Just to clarify the requirement, do you have some sort of list of the 'logtemp: ///filename1' items that you want to include under the do-action nodes?

Also, how do you want to do this? I assume from the topics that you want VB code, but you could also do this fairly easily with an xsl transformation if you're comfortable with that technology.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

JammerulesAuthor Commented:
There isn't necessarily a list of logtemp:///filename1. But depending on the availability, there could be multiple filenames.

Sadly, I am coding a vb program and this has to be done in vb alone.
andrewssd3Commented:
OK fine, you can code this using the xml DOM, and I can show you how.  First, what repeats in the input data - are there multiple do-actions within the request, or multiple requests each containing just one do-action?  It would probably help if you could post an example of the input data with repeating groups
JammerulesAuthor Commented:
So, in the below example, I have a xml file (test.xml)  with the following structure:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
	<env:Body>
		<dp:request domain="D" xmlns:dp="http://www.datapower.com/schemas">
			<dp:do-action> 
			</dp:do-action>
		</dp:request>
	</env:Body>
</env:Envelope>

Open in new window

So, in my vb code, assuming I have an array with  three items like this:

logtemp: ///filename1
logtemp: ///filename2
logtemp: ///filename3

I then will open the file test.xml and modify its structure to add the following xml code. In short,
1. For each item (logtemp: ///filename1) there will be a <DeleteFile> node
2. <DeleteFile> will have a child node logtemp:///filename1


Here is how the final test.xml will look like this
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
	<env:Body>
		<dp:request domain="D" xmlns:dp="http://www.datapower.com/schemas">
			<dp:do-action>
				<DeleteFile>
					<File>logtemp: ///filename1</File>
				</DeleteFile>
				<DeleteFile>
					<File>logtemp: ///filename2</File>
				</DeleteFile>
				<DeleteFile>
					<File>logtemp: ///filename3</File>
				</DeleteFile>
			</dp:do-action>
		</dp:request>
	</env:Body>
</env:Envelope>

Open in new window

:
andrewssd3Commented:
This code uses the Microsoft DOM model.  You will need a reference to 'Microsoft XML, V6.0'. I have put some comments in. Please let me know if you need any further explanation.
Option Explicit

Public Sub AddXMLActions()

    Const sINPUT_XML As String = "c:\users\stuart\desktop\sample.xml"
    Const sOUTPUT_XML As String = "c:\users\stuart\desktop\output.xml"

    Dim vFileNames As Variant
    
    Dim xmlDoc As DOMDocument60
    Dim oNode As IXMLDOMNode
    Dim oDeleteNode As IXMLDOMNode
    Dim oFileNode As IXMLDOMNode
    
    vFileNames = Array("logtemp: ///filename1", _
                        "logtemp: ///filename2", _
                        "logtemp: ///filename3")
    Dim vFileName As Variant


    Set xmlDoc = New DOMDocument60
    
    With xmlDoc
    
        .async = False
        .preserveWhiteSpace = True
        
        If Not .Load(sINPUT_XML) Then
            MsgBox .parseError.reason
            Exit Sub
        End If
    
    End With
    
    ' it's less efficient, but scan all elements to find the do-action node - this saves
    ' getting involved with namespaces - if you want to go straight to the do-action with
    ' an xpath query you would have to define both namespaces - I can show you how to
    ' do this, but with such a small file it's probably not worth the trouble
    
    For Each oNode In xmlDoc.DocumentElement.SelectNodes("//*")
        If oNode.BaseName = "do-action" Then
            For Each vFileName In vFileNames
                ' create the new nodes
                Set oDeleteNode = xmlDoc.createElement("DeleteFile")
                Set oFileNode = xmlDoc.createElement("File")
                
                oFileNode.Text = vFileName
                
                ' add the File element to the DeleteFile element
                oDeleteNode.appendChild oFileNode
                ' add the DeleteFile element to the do-action element
                oNode.appendChild oDeleteNode
                
            Next vFileName
        End If
    Next oNode
    
    ' save the new xml tree
    xmlDoc.Save sOUTPUT_XML

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JammerulesAuthor Commented:
Excellent! Thanks for the solution!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.