Page 1 of 1

HOW TO - Read XML and add to customized datagrid form

Posted: Mon Oct 28, 2013 11:22 am
by palanolho
Greetings everyone,

Dont know if this topic should go here but... here it goes.

I'm slowly working and learning more and more about livecode and XML and now I would need some help on how to get the all elements from the XML to populate a datagrid

so, I know I can find specific nodes on the tree, but how can I get the whole node to work it and add to the datagrid vector ?

let say I have this XML:

Code: Select all

<tokens>
	<single tknID="1">
		<name>New token 1</name>
	</single>
	<stack tknID="3">
		<name>New tokens stack 3</name>
	</stack>
	<stack tknID="4">
		<name>New tokens stack 4</name>
	</stack>
	<pool tknID="5">
		<name>New tokens pool 5</name>
	</pool>
	<single tknID="6">
		<name>New token 6</name>
	</single>
	<pool tknID="7">
		<name>New tokens pool 7</name>
	</pool>
</tokens>
So, my XML tree is in memory already and I have 3 different nodes inside the mail root node: single, stack

How could I read the XML to correctly add all nodes to the array and send it to the datagrid ? will I have to add one node at a time or can I add all nodes at once ?


Many thanks in advance,
- Miguel

Re: HOW TO - Read XML and add to customized datagrid form

Posted: Mon Oct 28, 2013 11:59 am
by bangkok
Add this library of functions into your stack

https://gist.github.com/trevordevore/5584753

Then, something like this... It's not very clean but it works.

Code: Select all

on mouseup 
put fld "yourXML" into tXML ----your source XML
   replace cr with "" in tXML
   replace "  " with "" in tXML
   
   put ConvertXMLToArray(tXML, "UTF-8") into theArray
   
   put SortArrayKeysWithXMLOrdering(theArray["tokens"]) into theKeys
   put 0 into i
   repeat for each line theKey in theKeys
          add 1 to i
          put theArray["tokens"][theKey] into theData[i]
   end repeat
   
   put  the number of lines of the keys of theData  into tNombre
   repeat with i = 1 to tNombre
      repeat with j = 1 to 2
         put theData[i]["name"]  into finalArray[i]["name"]
         put theData[i]["@attributes"]["tknID"] into finalArray[i]["tknID"]
      end repeat
   end repeat
   
   set the dgData of group "yourDatagrid" to finalArray  ---provided that your DG has 2 columns with "name" and "tknID" as names).
end mouseup

Re: HOW TO - Read XML and add to customized datagrid form

Posted: Mon Oct 28, 2013 12:04 pm
by palanolho
Greetings and thanks,

I have my tree in memory and nor in a field. can I use it anyway or do I have to convert it to text?
Also, I can't remove the spaces from the XML because I have some node values that have some texts with spaces. Will it work without removing the spaces?

Many thanks
- Migue

Re: HOW TO - Read XML and add to customized datagrid form

Posted: Mon Oct 28, 2013 12:06 pm
by bangkok
Yes with a variable it's okay.

For space, i put "replace double space", to keep your texts. Anyway, give it a try.