XML encoding oddities

Ken Ray kray at sonsothunder.com
Sun Nov 9 13:42:54 EST 2008


> 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 "<whatshappening></whatshappening>" 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:

<whatshappening>
    <name>
        fréd
    </name>
</whatshappening>

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: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the use-livecode mailing list