Micro Focus QTP (UFT) Forums
Inserting Value in XML - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Inserting Value in XML (/Thread-Inserting-Value-in-XML)



Inserting Value in XML - newinqtp - 10-28-2010

Hi

I am trying to insert multiple line in the XML using for loop. When I execute I am getting syntax error after Next stmt.

Code:
Input_request = "<soapenv:Envelope xmlns:soapenv="&chr(34)&">"_
   &"<soapenv:Header/>"_
   &"<soapenv:Body>"_
      &"<rm:itemSouring>"_
         &"<rm:itemNames>"
        For each x in rackNm
    Writeline "<rm:item>"&x&"</rm:item>"
            Next
            &"</rm:itemNames>"_
          &"</rm:itemSouring>"_
     &"</soapenv:Body>"_
&"</soapenv:Envelope>"


Can anyone tell me what is wrong.


RE: Inserting Value in XML - cdesserich - 11-01-2010

A couple of things are wrong here. First, Writeline is a function of the Scripting.FileSystemObject so you can't just call that function without its associated object. To concatenate strings to a variable in VBScript, you simply have to say var = var & "more string". Also, to get quotes in VBScript you simply have to type two together. If you are trying to get soapenv="" in your output, you have to double the quotes in the string constant "soapenv="""">". Seems silly, but that's how it works.

Code:
Dim rackNm, Input_request

rackNm = Array(1, 2, 3, 4, 5)

Input_request = "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
"<soapenv:Header />" & _
"<soapenv:Body>" & _
"<rm:itemSouring>" & _
"<rm:itemNames>"

For Each x In rackNm
Input_request = Input_request & "<rm:item>" & x & "</rm:item>"
Next

Input_request = Input_request & "</rm:itemNames>" & _
"</rm:itemSouring>" & _
"</soapenv:Body>" & _
"</soapenv:Envelope>"

MsgBox Input_request

If you are wanting to add the new line character to make the formatting look decent, then you might try something like this:

Code:
Dim rackNm, Input_request

rackNm = Array(1, 2, 3, 4, 5)

Input_request = "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">" & vbNewLine & _
vbTab & "<soapenv:Header />" & vbNewLine & _
vbTab & "<soapenv:Body>" & vbNewLine & _
vbTab & vbTab & "<rm:itemSouring>" & vbNewLine & _
vbTab & vbTab & vbTab & "<rm:itemNames>" & vbNewLine

For Each x In rackNm
Input_request = Input_request & vbTab & vbTab & vbTab & vbTab & "<rm:item>" & x & "</rm:item>" & vbNewLine
Next

Input_request = Input_request & vbTab & vbTab & vbTab & "</rm:itemNames>" & vbNewLine & _
vbTab & vbTab & "</rm:itemSouring>" & vbNewLine & _
vbTab & "</soapenv:Body>" & vbNewLine & _
"</soapenv:Envelope>"

MsgBox Input_request

Kind of messy, but it does the job.