revXML to Datagrid

Getting into LiveCode for iOS? Ask your questions here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
ctflatt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 240
Joined: Sun Dec 06, 2009 12:24 am
Location: Fredericktown, MO
Contact:

revXML to Datagrid

Post by ctflatt » Thu Mar 24, 2011 11:35 pm

Hello, all!

I spent the entire day playing with the new revXML external support for iOS.

The good news is it worked for me!!! Woohoo!!!

I am able to download a php-generated XML document from my server and parse the tree using revXML.

Has anyone seen or have experience with using XML to populate a Datagrid? I've checked the forums, but didn't find it. I've looked at DataGrid lessons on runrev.com, but they seem to be sqlite-specific...

Any info would be appreciated!

Thanks,

Todd

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 955
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: revXML to Datagrid

Post by trevordevore » Fri Mar 25, 2011 2:30 pm

Hi Todd,

To use XML with a data grid you just need to convert the XML to an array that you can assign to the dgData. This is what is going on in the lesson that converts a SQL cursor to an array:

http://lessons.runrev.com/spaces/lesson ... Grid-Array

You can use the revXML routines to iterate through the nodes in your XML tree and generate the array. I don't like using them myself so I wrote some handlers that convert XML to LiveCode arrays and vice versa. I then work with the array and convert back to XML when I'm done. You can find them in a stack I posted to RevOnline:

http://revonline2.runrev.com/stack/571/XMLAndArrays

Here is how you might use the handlers to create an array that you can assign to the dgData (untested).

Code: Select all

-- Sample XML
-- Assume this XML is in a variable named theXML.
<myxml>
    <node>value 1</node>
    <node>value 2</node>
    <node>value 3</node>
</myxml>

Code: Select all

-- Convert xml to array. Store data in array as UTF8.
put ConvertXMLToArray(theXML, "UTF-8") into theArray -- handler in RevOnline stack

-- Convert to an array that can be assigned to the dgData of a Data Grid (numerically indexed).
put SortArrayKeysWithXMLOrdering(theArray["myxml"]) into theKeys -- handler in RevOnline stack
put 0 into i
repeat for each line theKey in theKeys
    add 1 to i
    put theArray["myxml"][theKey] into theData[i]
end repeat

set the dgData of group "DataGrid" to theData
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: revXML to Datagrid

Post by BarrySumpter » Wed Jun 15, 2011 3:29 am

If anyone has a working sample I'd appreciate having it uploaded here so we can see it working.

I have a fair go at this a number of times but can't get the array to load into the grid.

I'm pretty sure its the column names on the Datagrid MUST be preset and match the Key names in the array.

I think the code behind the DataGrid needs to be updated to add the column names found in the array.

Anyone?
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Klaus
Posts: 11011
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: revXML to Datagrid

Post by Klaus » Wed Jun 15, 2011 4:17 pm

Hi Barry,
BarrySumpter wrote:... but can't get the array to load into the grid.
what exactrly happens?
Please post your script(s), we try hard but cannot read other peoples mind (yet).

What about the scripts that Trevor (who invented the DataGrid btw.!)?
Don't they work for you?
BarrySumpter wrote:I'm pretty sure its the column names on the Datagrid MUST be preset and match the Key names in the array.
Not necessarily!
BarrySumpter wrote:I think the code behind the DataGrid needs to be updated to add the column names found in the array.
No idea, but I guess not! 8)


Best

Klaus

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 955
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: revXML to Datagrid

Post by trevordevore » Wed Jun 15, 2011 4:24 pm

BarrySumpter wrote:I'm pretty sure its the column names on the Datagrid MUST be preset and match the Key names in the array.
Yes, when setting the dgData property the column names must already exist in the data grid.
BarrySumpter wrote:I think the code behind the DataGrid needs to be updated to add the column names found in the array.
No, this is by design. It is sometimes necessary to have keys in the dgData array that have no visual column associated with them. In addition, the order of the columns would not necessarily end up being the order you would want if the data grid did this automatically.

You can create columns by setting the dgProp["columns"] of group "DataGrid" to a line delimited list of your columns.
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: revXML to Datagrid

Post by BarrySumpter » Thu Jun 16, 2011 1:16 am

Thanks for the replies.
LOL.
Yeah that clarifies things I already know.
Feeling a bit better now after going to sleep unsatisfied after a very confusing day.
i.e. doughting my intellegence yet again. (happens more often that I'd like)

As a beginner I would prefer to have actual working samples.
I can make reasonable deductions myself then when nothing seems to work I ask.

----

Working with a datagrid and an array - NOT from XML
I'd like to load the grid with my array using field names from item x as column headers or OK keyNames.

Then later, when I want to play with the column properties I'll research how to do it.
Or later when I know how to use the datagrid properly
I can then load the columnheading properties then the griddata.
Like hiding my data by making a certain columnWidth 0 etc.

----

I've kinda given up on the XML to datagrid.
Or come to the realizatoin that MY XML tree data doesn't really fit in with a datagrid solution
To much complexity and no actual working example was a hard learn.

----

with my XML tree data - a large amount does NOT repeat so having heading at all doesn't work.
Which wouldn't work with a dataGrid

Then the next part DOES have monthly data and then daily data that I need to stay in the month/day order given in the XML tree data.
Which should work well with a dataGrid.

----

If anyone at all has a working example no matter how small or simple could you please post it?

I'm trying to use the XML to Array but the array comes out out of XML order.
i.e. I don't need it to sort the array.
I need the array in the exact order the XML was presented.

Also, that script working with the keys has nothing to work with and returns nothing.

So It's just theArray that I'm working with at the mo.

tia

Code: Select all

on mouseUp
  
-- I have no control over my data 
-- some days it works without mods 
-- other days it contains special characters and throws LiveCode XML routines off.
 
--   put field "txtField" into theXML
   
   local tPreferencesData, tResult
   put url ("file:" & "c:/myXML.xml") into tXMLData
   put the result into tResult
   if tResult is not empty then
      answer error "Failed to read XML Data file at location: " & "c:/myXML.xml"
      return empty
   end if
   
   
   
   -- Remove non-breaking spaces &nbsp; - 
   replace "&nbsp;" with empty in tXMLData
   
   -- Remove Delay tagged lines - always corrupted
   Put wordOffset("<delay",tXMLData) into tWordStartNum
   Put wordOffset("delay>",tXMLData) into tWordEndNum
   Delete word tWordStartNum to tWordEndNum of tXMLData
   
   --filter tXMLData without "*<delay*</delay>"   -- this works as well - thanks dglass
      
      put tXMLData into theXML
   
----   

   replace cr with empty in theXML
   
   -- Convert xml to array. Store data in array as UTF8.
   put ConvertXMLToArray(theXML, "UTF-8") into theArray -- handler in RevOnline stack
   

   -- Convert to an array that can be assigned to the dgData of a Data Grid (numerically indexed).
   --   put SortArrayKeysWithXMLOrdering(theArray["myxml"]) into theKeys -- handler in RevOnline stack
   --   put 0 into i
   --   repeat for each line theKey in theKeys
   --          add 1 to i
   --          put theArray["myxml"][theKey] into theData[i]
   --   end repeat
   
   put displayArrayData2 (theArray, "") into x
   replace " => Array" with empty in x
  
   put x into field txtField2  - much better formatting than the dataGrid in the exact example
   
   --combine theArray using return and tab 
   --   put theArray into field "Table Field"
   --   put theArray into field txtField2
   --   --put theArray into field DataGrid 1
   --   set the dgData of group "DataGrid 1" to theData
      
   --   lock screen
   

   --  This works setting up the column headings with Return as the delimiter
   --      put "Col1" & return & "Col21" & return & "Col3" into tColumnTitles 
   --      set the dgProp["columns"] of group "DataGrid 3" to tColumnTitles
   
--            NOTE: the GridData array Keys MUST match the colounHeading Names.
   
   --   ??? lost here with this exact XML implementation - may work better NOT using XML
   --   set the dgData of group "DataGrid" to theData --[??? entries]  
   --   unlock screen    

--         NOTE:  there is also a gdText property you can use
   
end mouseUp


function displayArrayData2 pArray, pIndent

   # create the variable that loops through the keys in the array
   local tKey
   if pArray is an array then
      # print information to indicate that we are entering a new nested level of the array
      get "Array" & return
      # print full stops that allow the reader to track the depth of an element
      --put "." after pIndent
      # create the indentation
      put tab after pIndent
      repeat for each key tKey in pArray
         # call displayArrayData with a nested array
         --put format("%s[%s] => %s\n", pIndent, tKey, displayArrayData2 (pArray[tKey], pIndent)) after it
         put format("%s%s => %s\n", pIndent, tKey, displayArrayData2 (pArray[tKey], pIndent)) after it
      end repeat
      delete the last char of it
      return it
   else
      return pArray
   end if

end displayArrayData2

All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: revXML to Datagrid

Post by BarrySumpter » Sat Jun 18, 2011 1:51 am

I'm trying to use the XML to Array but the array comes out - out of XML order.
Perhaps so, but the Node name is suffixed correctly with NodeName[27], NodeName[3], NodeName[15].
So I can use the [n] - number as a pointer to fill my own ListData or GridData.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Dougy
Posts: 15
Joined: Sat Jul 01, 2006 7:37 pm
Location: New Zealand

Re: revXML to Datagrid

Post by Dougy » Mon Jul 25, 2011 12:47 pm

Hi

I am also trying to populate a DataGrid with XML data and my code works fine, however LIveCode doesn't seem to like the "GSX" extended naming convention of XML, or am I doing something wrong?
My code is:

Code: Select all

local theData
repeat for each line tEntry in tEntries
      put revXMLNodeContents(pTree, "feed/"& tEntry &"/gsx:tuesday") after theData
end repeat
so.....

This line DOES work (XML "content" tag):
put revXMLNodeContents(pTree, "feed/"& tEntry &"/content") after theData

But this line DOESN'T work (XML "gsx: ..." tag):
put revXMLNodeContents(pTree, "feed/"& tEntry &"/gsx:monday") after theData

Any help appreciated! Thanks.
LC 6.6.3, AnimationEngine 5.1
BeatSpeak & EVPoint available for iOS devices on the App Store.

Dougy
Posts: 15
Joined: Sat Jul 01, 2006 7:37 pm
Location: New Zealand

Re: revXML to Datagrid

Post by Dougy » Wed Jul 27, 2011 6:55 am

Figured it out. If the XML tag is, for example, "GSX: name", you just remove the "GSX:" and use "name" by itself.

:-)
LC 6.6.3, AnimationEngine 5.1
BeatSpeak & EVPoint available for iOS devices on the App Store.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: revXML to Datagrid

Post by BarrySumpter » Wed Aug 03, 2011 7:30 am

Thats really strange.
I've returned to see where my sample of properly running sample is and didn't find any.

Does anyone have a actual sample of to post here?
I could have sworn I had one running and had posted the sample solution like most of my other posts.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

Re: revXML to Datagrid

Post by snm » Fri Feb 03, 2012 1:05 pm

How to make working XMLAndArrays.rev example stack with unicode (spec. accented letters missing in the upper ASCI as łćźżą etc. - Polish accented chars)

snm

Post Reply

Return to “iOS Deployment”