Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XML Data read and extract output in an array
#1
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Reply
#2
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#3
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Reply
#4
Solved: 11 Years, 3 Months, 4 Weeks ago
I do not get the </ABC> for the xml data you had provided. Can you please highlight it?
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#5
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#6
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#7
Solved: 11 Years, 3 Months, 4 Weeks ago Shy 
It' perfect Smile
Thanks a ton !!!
Reply
#8
Solved: 11 Years, 3 Months, 4 Weeks ago
Most Welcome Smile
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#9
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#10
Solved: 11 Years, 3 Months, 4 Weeks ago
Thanks Tanya for the above code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to extract particular digit from a string amit25007 3 3,484 10-05-2016, 10:49 AM
Last Post: Ankur
  Reading XML Data from a WebPage shaan.mishra87@gmail.com 1 2,221 09-28-2016, 09:16 PM
Last Post: supputuri
  table output values shipu 1 2,341 01-24-2014, 10:06 AM
Last Post: supputuri
  How to extract part of a string between two particular characters. ACCBAJPA 7 5,787 08-22-2013, 02:39 PM
Last Post: ACCBAJPA
  Getting an 'Error in XML document' Error when submitting web services XML luckyexpert 0 3,133 08-20-2013, 10:23 PM
Last Post: luckyexpert

Forum Jump:


Users browsing this thread: 4 Guest(s)