Solved

xml and vb6: Add nodes at every repeating level

Posted on 2013-12-29
10
364 Views
Last Modified: 2014-01-03
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? :)
0
Comment
Question by:Jammerules
  • 5
  • 3
10 Comments
 

Author Comment

by:Jammerules
ID: 39747365
Bump!
0
 

Author Comment

by:Jammerules
ID: 39751414
Wondering if this question is that difficult that nobody made a comment yet!! o_O
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39753579
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.
0
 

Author Comment

by:Jammerules
ID: 39754101
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:andrewssd3
ID: 39754123
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
0
 

Author Comment

by:Jammerules
ID: 39754181
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

:
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 39754349
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

0
 

Author Comment

by:Jammerules
ID: 39755127
Excellent! Thanks for the solution!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
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…

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

13 Experts available now in Live!

Get 1:1 Help Now