Problem with Reading XML Data

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Problem with Reading XML Data

Post by pkmittal » Mon Apr 13, 2015 12:26 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3582
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Problem with Reading XML Data

Post by mwieder » Mon Apr 13, 2015 7:31 pm

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.

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Problem with Reading XML Data

Post by pkmittal » Tue Apr 14, 2015 5:11 am

Thanks for your help.. Now I have understood how it works,

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3582
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Problem with Reading XML Data

Post by mwieder » Tue Apr 14, 2015 6:41 am

Great. Glad it helped. This can definitely be intimidating coming into it for the first time.

Post Reply