[转]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 &gt; b then
        return a;
    else
        return b;
    end
end
</script>

大于号   > 要写成 &gt;  , 就是greater than的意思。同样:
小于号   <                &lt;            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文件里时,忘了把&gt;之类的符号换成 > 等,导致我查了很久,老子还跟踪到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 &lt; 1000 then</message>

There are 5 predefined entity references in XML:

&lt;
<
less than

&gt;
>
greater than

&amp;
&
ampersand

&apos;
'
apostrophe

&quot;
"
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.

转载于:https://www.cnblogs.com/pulas/archive/2012/05/17/2506713.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值