Solved

xml and vb6: Add nodes at every repeating level

Posted on 2013-12-29
10
380 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

756 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