Micro Focus QTP (UFT) Forums
XML Data read and extract output in an array - 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: XML Data read and extract output in an array (/Thread-XML-Data-read-and-extract-output-in-an-array)



XML Data read and extract output in an array - SweetyChowdhury - 04-08-2013

Hi All,

Please see the XML file format that I need to read and extract the value of a particular node (have multiple elements under it) in an array through QTP 11v.

Code:
<MasterData>
<Version 1.0>
<SerialNumer> 1111 </SerialNumber>
<FirstData>
  <Item 1>
  <Item 2>
    <FAT>...</FAT>
    <THIN>...</THIN>
    <TOOL>
      <VersionNumber>1</VersionNumber>
      <Type>0</Type>
      <ListNumber>0</ListNumber>
   </TOOL>
   <BOOK>
      <Reason>0</Reason>
      <Number>1</Number>
      <Load>0</Load>
   </BOOK>    
</FirstData>
<SecondData>
<Item 1>
<Item 2>
</SecondData>  
</MasterData>
From the above XML file ,I need to get the element values under the node TOOL and BOOK into 2 separate arrays.
Can somebody please help on the same.


RE: XML Data read and extract output in an array - basanth27 - 04-09-2013

does this help?
Code:
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\testme.txt",1)
strFileText = objFileToRead.ReadAll()
msgbox strFileText
objFileToRead.Close
Set objFileToRead = Nothing
ToolNode = Split(Split(strFileText,"<TOOL>")(1),"</TOOL>")(0)
msgbox ToolNode
BookNode = Split(Split(strFileText,"<BOOK>")(1),"</BOOK>")(0)
msgbox BookNode



RE: XML Data read and extract output in an array - SweetyChowdhury - 04-09-2013

Thanks Basanth for the above code. It defonatley gives me the element name and its value.
Just one more doubt how can remove the </ABC> for all the elements coming in the result.


RE: XML Data read and extract output in an array - basanth27 - 04-09-2013

I do not get the </ABC> for the xml data you had provided. Can you please highlight it?


RE: XML Data read and extract output in an array - SweetyChowdhury - 04-09-2013

Thanks Basanth for the above code. It defonatley gives me the element name and its value.
Just one more doubt how can remove the </ABC> for all the elements coming in the result.
Quote:I do not get the </ABC> for the xml data you had provided. Can you please highlight it?

Sorry for the confusion. Now from the above code which you provided, I am getting the result as :
<Reason>0</Reason>
<Number>1</Number>
<Load>0</Load>

And I want my result to look as below :

<Reason>0
<Number>1
<Load>0

I hope that clarifies.
Thanks


RE: XML Data read and extract output in an array - basanth27 - 04-09-2013

All good ? Smile

Code:
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\testme.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
Set objFileToRead = Nothing
ToolNode = Split(Trim(Split(Split(strFileText,"<TOOL>")(1),"</TOOL>")(0)),vbNewLine)

BookNode = Split(Trim(Split(Split(strFileText,"<BOOK>")(1),"</BOOK>")(0)),vbNewLine)

For intBCounter = Lbound(BookNode) to Ubound(BookNode)
  If Len(BookNode(intBCounter)) <> 0 Then
    msgbox Split(BookNode(intBCounter),"</")(0)
  End If
Next

For intTCounter = Lbound(ToolNode) to Ubound(ToolNode)
  If Len(ToolNode(intTCounter)) <> 0 Then
    msgbox Split(ToolNode(intTCounter),"</")(0)
  End If
Next
Hopefully this helps, let me know if you need any clarifications.


RE: XML Data read and extract output in an array - SweetyChowdhury - 04-10-2013

It' perfect Smile
Thanks a ton !!!


RE: XML Data read and extract output in an array - basanth27 - 04-10-2013

Most Welcome Smile


RE: XML Data read and extract output in an array - tanyamrz - 04-25-2013

Hi Sweety, for the deletion of xml data i was not able to reply to that post, instead of that iam replying the answer here

Code:
Set xmlDoc =   CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"
xmlDoc.Load("C:\test.xml")

Set colNodes=xmlDoc.selectNodes  ("/Fileheaderproperty/type")

For Each objNode in colNodes

If instr(Ucase(objNode.text),"YES") Then
  xmlDoc.documentElement.removeChild (objNode)
  End If
Next
  
xmlDoc.Save "C:\test1.xml"


The Out put will be Like
Code:
- <Fileheaderproperty>
- <type>
  <Car>2</Car>
  <parking>no</parking>
  <rent>560</rent>
  </type>
- <type>
  <Car>3</Car>
  <parking>no</parking>
  <rent>750</rent>
  </type>
- <type>
  <Car>5</Car>
  <parking>no</parking>
  <rent>260</rent>
  </type>
  </Fileheaderproperty>

Thanks
-Tanya


RE: XML Data read and extract output in an array - SweetyChowdhury - 05-10-2013

Thanks Tanya for the above code.