writing to a XML Node -> node is empty

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
okk
Posts: 176
Joined: Wed Feb 04, 2015 11:37 am

writing to a XML Node -> node is empty

Post by okk » Wed Feb 26, 2020 12:23 pm

Hi,

I have created an xml tree with all the nodes. I want to overwrite the text in a node. I noticed that when I use an Umlaut the revXMLPutIntoNode command creates an empty node (example 1). Without an Umlaut it works (example 2).

Code: Select all

(1) revXMLPutIntoNode treeID,"speech/speechname","äiti"

Code: Select all

(2) revXMLPutIntoNode treeID,"speech/speechname","aiti"
I know that I can use textencode

Code: Select all

revXMLPutIntoNode treeID,"speech/speechname",textencode("äiti", "UTF8")
But then my XML file looks like:

Code: Select all

<?xml version="1.0"?>
<speech>
	<speechname>&#xE4;iti</speechname>
But I would prefer that it looks like:

Code: Select all

<?xml version="1.0"?>
<speech>
	<speechname>Äiti</speechname>
I would prefer that the XML stays human readable as much as possible in a simple text program like TextEdit. Is this a silly idea? If not, how can I achieve this? Btw. Äiti is Finnish and means Mother, in case you wanted to know... Best. Oliver

okk
Posts: 176
Joined: Wed Feb 04, 2015 11:37 am

Re: writing to a XML Node -> node is empty

Post by okk » Tue Mar 17, 2020 4:41 pm

Hi, I posted the same question on the runrev list and got a solution from Mark:

Code: Select all

on mouseUp
   local tInitialXML, tXmlId
   put "<?xml version='1.0' encoding='UTF-8'?><identity><name></name></identity>" into tInitialXML
   put revXMLCreateTree(tInitialXML,false,true,false) into tXmlId
   revXMLPutIntoNode tXmlId, "/identity/name",  textEncode("Äiti", "utf-8")
   put revXMLText(tXmlId) into URL ("binfile:"&specialFolderPath("desktop")&slash&"myFile.xml")
end mouseUp
Mark Waddingham wrote furthermore as explanation:
"You have to create the XML tree with an initial `<?xml` line so that libxml2 knows what encoding you want - utf-8 will give what the use-cases here have requested. Then, you have to textEncode any text you use as utf-8 before passing it to the command/function (usually just content, but also attributes, element names or attribute values - if any may contain non-ascii text). Finally, revXMLText will return an already encoded bit of binary data you can put into a file - hence binfile."

I tested it and it works exactly as intended. Thanks Mark. I just thought to post it here as well for those who missed it on the runrev list.

If I understood it correctly my mistake was that when initialising the xml tree I did not explicitley tell that I want the xml tree to use utf-8 encoding.


Best
Oliver

Post Reply

Return to “Talking LiveCode”