[转]xml中嵌入lua
http://www.cppblog.com/lai3d/archive/2007/06/23/26849.html
学xml http://www.w3schools.com/xml/default.asp
我们游戏里界面和与界面有关的很多逻辑都是写在xml里面的,其中逻辑使用lua写,嵌在xml中,用xmlSpy编辑。
xml中嵌入lua,比如
<script>
function max(a,b)
if a > b then
return a;
else
return b;
end
end
</script>
大于号 > 要写成 > , 就是greater than的意思。同样:
小于号 < < less than
这样子lua没有高亮显示,搞得我看起来很痛苦,晚上突发奇想,从*.lua文件里读,就可以用LuaEdit来编辑了,那我就可以按F6来check syntax了,哈哈
<script luafile="1" path="gui\haha.lua">
</script>
luafile 为 1,就读path里的,为0就读script节点的text,哈哈,爽!
我从xml文件中把lua段粘贴到*.lua文件里时,忘了把>之类的符号换成 > 等,导致我查了很久,老子还跟踪到luabind和lua的源代码里面去,靠!faint!
幸亏LuaEdit有Check Syntax功能,帮助我检查到了错误,深刻体会到了工具的好处,也更加坚信“人与动物的最大差别在于人类能够制造工具并使用工具”!
要是再整得能单步调试,那就爽歪歪了!
All text in an XML document will be parsed by the parser.
Only text inside a CDATA section will be ignored by the parser.
Parsed Data
XML parsers normally parse all the text in an XML document.
When an XML element is parsed, the text between the XML tags is also parsed:
<message>This text is also parsed</message>
The parser does this because XML elements can contain other elements, as in this example, where the <name> element contains two other elements (first and last):
<name><first>Bill</first><last>Gates</last></name>
and the parser will break it up into sub-elements like this:
<name> <first>Bill</first> <last>Gates</last> </name>
Escape Characters
Illegal XML characters have to be replaced by entity references.
If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element. You cannot write something like this:
<message>if salary < 1000 then</message>
To avoid this, you have to replace the "<" character with an entity reference, like this:
<message>if salary < 1000 then</message>
There are 5 predefined entity references in XML:
<
<
less than
>
>
greater than
&
&
ampersand
'
'
apostrophe
"
"
quotation mark
Note: Only the characters "<" and "&" are strictly illegal in XML. Apostrophes, quotation marks and greater than signs are legal, but it is a good habit to replace them.
CDATA
Everything inside a CDATA section is ignored by the parser.
If your text contains a lot of "<" or "&" characters - as program code often does - the XML element can be defined as a CDATA section.
A CDATA section starts with "<![CDATA[" and ends with "]]>":
<script> <![CDATA[ function matchwo(a,b) { if (a < b && a < 0) then { return 1 } else { return 0 } } ]]> </script>
In the example above, everything inside the CDATA section is ignored by the parser.
Notes on CDATA sections:
A CDATA section cannot contain the string "]]>", therefore, nested CDATA sections are not allowed.
Also make sure there are no spaces or line breaks inside the "]]>" string.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构