Re: XML encoding oddities
Trevor, this is nice. I'd started doing something similar, but then realised that what I was after for what I'm working on was a way to neatly package up pretty much any non-binary data, array or not, whatever platform, and send to to a linux server (also running revolution) where it could be unpacked and used as is - hence the encoding issue. What I've come up with is considerably longer than yours :-) (Partly because I've factored out quite a lot of stuff to sub-handlers). But it seems to work well, and even produces slightly more compact output than yours when dealing with chunks of data 2-3Kb or more in size, which is no bad thing even in today's "bandwidth? what's bandwidth?" world. I also wrote a handy function arraysMatch() to test the input/output. For what it's worth, here it all is, below, Best, Mark -- pack and unpack are the 'high-level' functions function pack pData if pData is not an array then put pData into tArray["futpak"] put tArray into pData end if put atx(pData) into tXml put base64encode(compress(tXml)) into tB64data replace cr with empty in tB64data return "futpak" & tB64data end pack - function unpack pData if char 1 to 6 of pData is not "futpak" then return empty put decompress(base64decode(char 7 to -1 of pData)) into tXml put xta(tXml) into tArray if keys(tArray) = "futpak" then return tArray["futpak"] else return tArray end if end unpack -- -- turns any revolution array into xml. (not nice to look at, though) -- array keys are stored as attributes of nodes -- all element and attribute contents are utf8encoded and base64encoded -- function atx pArray put "" after tXml put atxNodes(pArray) after tXml put "" after tXml return tXml end atx private function atxNodes pArray pLevel if pLevel is empty then put 0 into pLevel add 1 to pLevel repeat for each key k in pArray add 1 to n put "atx" & pLevel & "-" & n into tName put "<" & tName && "key=" & q(b64U8encode(k)) & ">" after tXml if pArray[k] is an array then put atxNodes(pArray[k], pLevel) after tXml else put b64U8encode(pArray[k]) after tXml end if put "" after tXml end repeat return tXml end atxNodes --- -- takes xml and attempts to turn it into an array -- see atx() above. --- function xta pXml put revCreateXmlTree(pXml, true, true, false) into tTree put xtaNodes(tTree, "revarray") into tArray revDeleteXmlTree tTree return tArray end xta - private function xtaNodes pTree, pNode put revXmlFirstChild(pTree, pNode) into tNode repeat while tNode is not empty and "xmlerr" is not in tNode put b64U8decode(revXmlAttribute(pTree, tNode, "key")) into tKey if revXmlChildNames(pTree, tNode,cr,, false) is empty then put b64U8decode(revXmlNodeContents(pTree, tNode)) into tArray[tKey] else put xtaNodes(pTree, tNode) into tArray[tKey] end if put revXmlNextSibling(pTree, tNode) into tNode end repeat return tArray end xtaNodes - function utf8encode pString return unidecode(uniencode(pString),"UTF8") end utf8encode - function utf8decode pString return unidecode(uniencode(pString,"UTF8")) end utf8decode - function b64U8encode pData put base64encode(utf8encode(pData)) into tEnc replace cr with empty in tEnc return tEnc end b64U8encode - function b64U8decode pData return utf8decode(base64decode(pData)) end b64U8decode - function arraysMatch a1, a2 put keys(a1) into k1 ; put keys(a2) into k2 sort lines of k1; sort lnes of k2 put (k1 = k2) into tArraysMatch if tArraysMatch then repeat for each key k in a1 if a1[k] is an array then put arraysMatch(a1[k], a2[k]) into tArraysMatch else put (a1[k] = a2[k]) into tArraysMatch end if if not tArraysMatch then exit repeat end repeat end if return tArraysMatch end arraysMatch - function q aString return quote & aString & quote end q ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: XML encoding oddities
On Nov 10, 2008, at 11:21 AM, Mark Smith wrote: Thanks Trevor! (Also Ken). That seems to work very well. The particular problem I was trying to solve was how to serialize arrays with arbitrary keys and contents, such that they can be shared across networks and platforms. I think I've found a solution for my purposes, (though it won't be reliable with binary data), and it involves building the xml without the xml library, and then using the library to unserialize - though it fails if there is any "xml..." header at all! Mark, For what it's worth I've been using the attached SerializeArray/ UnserializeArray handlers with relatively small arrays for a while now and they seem to be working well enough for storing them in a database. Regards, -- Trevor DeVore Blue Mango Learning Systems ScreenSteps: http://www.screensteps.com Developer Resources: http://revolution.bluemangolearning.com function SerializeArray pArray repeat for each key theKey in pArray if the keys of pArray[theKey] is not empty then put urlencode(theKey) & ":" & "___array___:" & urlencode(SerializeArray(pArray[theKey])) & cr after theData else put urlencode(theKey) & ":" & urlencode(base64encode(pArray[theKey])) & cr after theData end if end repeat delete the last char of theData return theData end SerializeArray -- Returns array -- Support Rev 3.0 multi-dimensional arrays function UnserializeArray pSerializedArray set the itemdelimiter to ":" repeat for each line theLine in pSerializedArray if item 2 of theLine begins with "___array___" then put urldecode(item 1 of theLine) into theKey put UnserializeArray(urldecode(item 3 of theLine)) into theArray[theKey] else put urldecode(item 1 of theLine) into theKey put base64decode(urldecode(item 2 to -1 of theLine)) into theArray[theKey] end if end repeat return theArray end UnserializeArray ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: XML encoding oddities
Thanks Trevor! (Also Ken). That seems to work very well. The particular problem I was trying to solve was how to serialize arrays with arbitrary keys and contents, such that they can be shared across networks and platforms. I think I've found a solution for my purposes, (though it won't be reliable with binary data), and it involves building the xml without the xml library, and then using the library to unserialize - though it fails if there is any "header at all! Best, Mark On 10 Nov 2008, at 14:33, Trevor DeVore wrote: On Nov 9, 2008, at 11:50 AM, Mark Smith wrote: So, finally, is there a way to encode xml documents as UTF-8 (or whatever) without having to encode each part myself, and add the encoding attribute to the header myself? Mark, Have you tried creating your XML tree with the encoding included? put format(">") into tXml put revCreateXmlTree(tXml, true, true, false) into tTree put revXmlRootNode(tTree) into tNode I've done this in the past and then I pass UTF-8 encoded strings to the revXML handlers. Regards, -- Trevor DeVore Blue Mango Learning Systems ScreenSteps: http://www.screensteps.com Developer Resources: http://revolution.bluemangolearning.com ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: XML encoding oddities
On Nov 9, 2008, at 11:50 AM, Mark Smith wrote: So, finally, is there a way to encode xml documents as UTF-8 (or whatever) without having to encode each part myself, and add the encoding attribute to the header myself? Mark, Have you tried creating your XML tree with the encoding included? put format(">") into tXml put revCreateXmlTree(tXml, true, true, false) into tTree put revXmlRootNode(tTree) into tNode I've done this in the past and then I pass UTF-8 encoded strings to the revXML handlers. Regards, -- Trevor DeVore Blue Mango Learning Systems ScreenSteps: http://www.screensteps.com Developer Resources: http://revolution.bluemangolearning.com ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: XML encoding oddities
> I forgot that there was a whole 'nother XML library to try. I gave up on the > RunRev one. Does the stsXML library return nicely formated XML that is all > indented, nested, spaces inserted (or tabs) and everything so it is clean to > read? Yes, as a matter of fact! > Did you stop working on it now that RunRev has all their XML stuff or > are you still improving it as an alternative? I'm still working on it (off and on), but haven't had time to do much more than maintain it at the moment. Ken Ray Sons of Thunder Software, Inc. Email: [EMAIL PROTECTED] Web Site: http://www.sonsothunder.com/ ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: XML encoding oddities
I just noticed in re-reading the post "more human readable" -- what a nice feature. I'm so mad now that I wrote my whole XML output all by hand and I could have used your library. On Sun, Nov 9, 2008 at 2:58 PM, william humphrey <[EMAIL PROTECTED]>wrote: > I forgot that there was a whole 'nother XML library to try. I gave up on > the RunRev one. Does the stsXML library return nicely formated XML that is > all indented, nested, spaces inserted (or tabs) and everything so it is > clean to read? Did you stop working on it now that RunRev has all their XML > stuff or are you still improving it as an alternative? -- http://www.bluewatermaritime.com ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: XML encoding oddities
I forgot that there was a whole 'nother XML library to try. I gave up on the RunRev one. Does the stsXML library return nicely formated XML that is all indented, nested, spaces inserted (or tabs) and everything so it is clean to read? Did you stop working on it now that RunRev has all their XML stuff or are you still improving it as an alternative? ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: XML encoding oddities
> Ken (if you're reading this), does your library deal with this stuff > better? Well, I wouldn't necessarily say *better*, but just *different*. My library will allow encoded characters as contents or attributes for nodes without issue, but won't automatically add the XML Declaration to the tree if you are loading an existing XML structure (my library only adds the XML Declaration if you start a new document from scratch and add nodes through script, and even then, the declaration doesn't provide any encoding attributes). Also, the output is by default more human-readable: - on mouseUp put toXml() into tXml put tXml & cr & cr & fromXml(tXml) end mouseUp function toXml put "" into tXml put stsXML_LoadData(tXml) into tTree put stsXML_GetRoot(tTree) into tNode get stsXML_AppendChild(tNode,"ELEM","name","fréd") put stsXML_expand(tNode) into tText get stsXML_DeleteDocument(tTree) return tText end toXml function fromXml pXml put stsXML_LoadData(pXml) into tTree put stsXML_GetRoot(tTree) into tNode put stsXML_GetFirstChild(tNode) into tChild put stsXML_getNodePath(tChild) into tChild put stsXML_GetNodeData(tChild) into tContent get stsXML_DeleteDocument(tTree) return tChild & cr & tContent end fromXml - The output is: fréd 1/whatshappening/name fréd So in this case, you'd have to prepend the XML it returned with your own string for the XML declaration (but then again, you'd have to know what encoding you wanted in the first place). Ken Ray Sons of Thunder Software, Inc. Email: [EMAIL PROTECTED] Web Site: http://www.sonsothunder.com/ ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
XML encoding oddities
I'm seeing some (I think) very strange behaviour from the XML library... (warning, this is quite long, and won't be of much interest to anyone who isn't using the library...) This is on an intel macintosh, OS 10.4.11 In a button, I have the following script: - on mouseUp put toXml() into tXml put tXml & cr & fromXml(tXml) end mouseUp function toXml put "" into tXml put revCreateXmlTree(tXml, true, true, false) into tTree put revXmlRootNode(tTree) into tNode revAddXmlNode tTree, tNode, "name", "fred" put revXmlText(tTree) into tText revDeleteXmlTree tTree return tText end toXml function fromXml pXml put revCreateXmlTree(pXml, true, true, false) into tTree put revXmlRootNode(tTree) into tNode put revXmlFirstChild(tTree, tNode) into tChild put revXmlNodeContents(tTree, tChild) into tContent revDeleteXmlTree(tTree) return tChild & cr & tContent end fromXml - The output is: fred /whatshappening/name fred So all is good. If I change "fred" in the toXml function to "fréd", (acute accent on the 'e'), I get this: /whatshappening/name The content has simply disappeared, so I guess I need to encode non- ascii material. OK, but as what? (ideally UTF-8), and how do I indicate what I've done in my xml document? However, if I now add an accented string as an attribute: - function toXml put "" into tXml put revCreateXmlTree(tXml, true, true, false) into tTree put revXmlRootNode(tTree) into tNode revAddXmlNode tTree, tNode, "name", "fred" revSetXmlAttribute tTree, tNode & "/name", "orig", "fréd" put revXmlText(tTree) into tText revDeleteXmlTree tTree return tText end toXml function fromXml pXml put revCreateXmlTree(pXml, true, true, false) into tTree put revXmlRootNode(tTree) into tNode put revXmlFirstChild(tTree, tNode) into tChild put revXmlNodeContents(tTree, tChild) into tContent put revXmlAttribute(tTree, tChild, "orig") into tAtt revDeleteXmlTree(tTree) return tChild & cr & tContent & cr & tAtt end fromXml - I get: fred /whatshappening/name fred fr¬éd An encoding attribute has now been aded to the xml header, and some version of the "orig" attribute value (not ISO-8859-1, as far as I can tell) has been produced. So, finally, is there a way to encode xml documents as UTF-8 (or whatever) without having to encode each part myself, and add the encoding attribute to the header myself? What is slightly worrying is that it seems the library will add an encoding attribute to the header in some circumstances, but not others. Ken (if you're reading this), does your library deal with this stuff better? Best, Mark ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution