Solved

xml and vb6: Add nodes at every repeating level

Posted on 2013-12-29
10
382 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
[X]
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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migration from SQL server to oracle (XML input) 4 65
statistics basics sources 2 70
VB6 calling COM DLL written in Visual Studio 2003 6 52
Notepad++ Question 11 21
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 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