当我使用luaxml来解析XML字符串时,我对行为感到困惑.Lua doc声明在表变量上调用print():
print(type(t))
print(t)
Run Code Online (Sandbox Code Playgroud)
将产生如下输出:
t2: table
t2: table: 0095CB98
Run Code Online (Sandbox Code Playgroud)
但是,当我使用luaxml时:
require "luaxml"
s = "<a> <first> 1st </first> <second> 2nd </second> </a>"
t = xml.eval(s)
print("t: ", type(t))
print("t: ", t)
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
t: table
t: <a>
<first>1st</first>
<second>2nd</second>
</a>
Run Code Online (Sandbox Code Playgroud)
为什么不print(t)返回看起来像第一个例子的结果?
该print函数用于tostring将其参数转换为字符串.
当tostring用表调用,并且表的metatable有一个__tostring字段时,然后tostring使用表作为参数调用相应的值,并使用调用的结果作为结果.
我怀疑__tostringluaxml在返回的表上有这样一种metamethod xml.eval(s).
| 归档时间: |
|
| 查看次数: |
263 次 |
| 最近记录: |