Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

xml and vb6: Add nodes at every repeating level

Posted on 2013-12-29
10
379 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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