Lua, XML, UTF-8

I use luaxml lib to generate xml files after selecting a database from lua tables. Everything is fine, but I use Russian characters in my database (NySQL). What I need to do with luaxml is not representing these characters with codes (a-la ì), but with real characters. I found the function of the xml.registerCode method (decoded, encoded), but did not understand anything: (

Or maybe I need to use a different library. And if so - what is lib?

+4
source share
2 answers

Do you mean Russian letters?

Do you have UTF-8 in an XML file? maybe you can try windows-1251 if you only show russian text?

Or check the encoding of the MySQL table, if you see some things, such as (a-la), you have to change the encoding for the table itself.

I had the same problem with Georgian letters in MySQL, they were changed to 14124 # 121 # smth, as after inserting into a table in MySQL, I changed the encoding for the table and it was fixed.

0
source

I looked inside lib - it does strong encoding for all> 127 bytes, thereby breaking UTF into separate characters. It does this after using the built-in .registerCode mechanism, so you cannot even override it.

If you need to encode some complex data structure, you can simply expand all these object substitutions after the completion of XmlLua , declaring somewhere:

 local high_ascii_unroll = {} for code = 128, 255 do high_ascii_unroll['&#' .. code .. ';'] = string.char(code) end 

and then using gsub in the final line:

 local doc = xml.new("outer") doc.version = "2.0" local inner = xml.new("inner") inner.id = "" table.insert(doc, inner) local encoded = xml.str(doc):gsub('&#%d+;', high_ascii_unroll) -- <outer version="2.0"> -- <inner id="" /> -- </outer> 
0
source

Source: https://habr.com/ru/post/1333652/


All Articles