Page 1 of 1

Problem with Reading XML Data

Posted: Mon Apr 13, 2015 12:26 pm
by pkmittal
Hi, I am having some difficuly in reading XML DATA in Live Code.

Here is the XML DATA

Code: Select all

<result>
<allPostIts>
<postit>
<username>pradeep1</username>
<postit>
<![CDATA[ My Value 1 ]]>
</postit>
</postit>
<postit>
<username>pradeep1</username>
<postit>
<![CDATA[ My Value 2 ]]>
</postit>
</postit>
<postit>
<username>pradeep1</username>
<postit>
<![CDATA[ My Value 3 ]]>
</postit>
</postit>
<postit>
<username>pradeep2</username>
<postit>
<![CDATA[ My Value 4 ]]>
</postit>
</postit>
<postit>
<username>pradeep2</username>
<postit>
<![CDATA[ My Value 5 ]]>
</postit>
</postit>
<postit>
<username>pradeep2</username>
<postit>
<![CDATA[ My Value 6 ]]>
</postit>
</postit>
<postit>
<username>pradeep3</username>
<postit>
<![CDATA[ My Value 7 ]]>
</postit>
</postit>
<postit>
<username>pradeep3</username>
<postit>
<![CDATA[ My Value 8 ]]>
</postit>
</postit>
<postit>
<username>pradeep3</username>
<postit>
<![CDATA[ My Value 9 ]]>
</postit>
</postit>
</allPostIts>
</result>
What would the few lines of code to read the values of individual user names and their PostIt Values? It must be one or 2 line of code.. but I am not able to understand it

Please help.

Thanks

Re: Problem with Reading XML Data

Posted: Mon Apr 13, 2015 7:31 pm
by mwieder
Well, I don't think it's possible in one or two lines, but here's the most compact form I could come up with based on your example data (assuming here that the data is in field 1):

Code: Select all

on mouseUp pMouseBtnNo
   local tXMLData
   local tXMLTree
   local tNode
   local tHowManyNodes
   
   put field 1 into tXMLData
   put revXMLCreateTree(tXMLData, true, true) into tXMLTree
   put revXMLFirstChild(tXMLTree, "/") into tNode
   put revXMLNumberOfChildren(tXMLTree, tNode, "username",-1) into tHowManyNodes
   put revXMLFirstChild(tXMLTree, tNode) into tUser
   repeat with x=1 to tHowManyNodes
      put revXMLNodeContents(tXMLTree, tUser & "/username") & cr after msg
      put revXMLNodeContents(tXMLTree, tUser & "/postit") & cr after msg
      put revXMLNextSibling(tXMLTree, tUser) into tUser
   end repeat
end mouseUp
I also think it's bad form to create multiple levels with identical names ("postit"), but that's a matter of style.

Re: Problem with Reading XML Data

Posted: Tue Apr 14, 2015 5:11 am
by pkmittal
Thanks for your help.. Now I have understood how it works,

Re: Problem with Reading XML Data

Posted: Tue Apr 14, 2015 6:41 am
by mwieder
Great. Glad it helped. This can definitely be intimidating coming into it for the first time.