|
-- 获取源文件信息
|
local handle = io.popen("cd")
|
local script_dir
|
if handle then
|
script_dir = handle:read("*a"):gsub("[\r\n]+$", "")
|
handle:close()
|
end
|
print(jit and jit.arch or "unknown")
|
-- 设置脚本目录
|
--package.path = package.path .. ";./bin/;./jx/?.lua;./jx_base/?.lua;./mobox_base/?.lua;./wms/?.lua;./wms_base/?.lua;"
|
-- 动态获取当前脚本目录(关键!)
|
--local script_dir = debug.getinfo(1, "S").source:match("@?(.*/)")
|
print("当前脚本所在的目录是: " .. script_dir)
|
|
-- 重置 package.path(避免旧路径干扰)
|
package.path = ""
|
package.path = package.path .. ";" .. script_dir .. "./?.lua"
|
-- 添加必要路径(使用正确的模板格式)
|
package.path = package.path ..
|
";" .. script_dir .. "/bin/?.lua" .. -- 子目录 bin
|
";" .. script_dir .. "/lualibs/?.lua" .. -- 子目录 bin
|
";" .. script_dir .. "/jx/?.lua" .. -- 子目录 jx
|
";" .. script_dir .. "/jx_base/?.lua" ..
|
";" .. script_dir .. "/wms/?.lua" ..
|
";" .. script_dir .. "/lualibs/xml2lua-master/?.lua" ..
|
";" .. script_dir .. "/wms_base/?.lua"..
|
";" .. script_dir .. "/mobox_base/?.lua"
|
|
-- 添加 C 模块搜索路径(针对 Windows 的 .dll 文件)
|
package.cpath = package.cpath .. ";"
|
.. script_dir .. "/bin/clibs/?.dll"..
|
";" .. script_dir .. "/bin/socket/?.dll"..
|
";" .. script_dir .. "/bin/clibs/socket/?.dll"
|
|
local xml2lua = require("xml2lua")
|
--Uses a handler that converts the XML to a Lua table
|
local handler = require("xmlhandler.tree")
|
|
local xml = [[
|
<people>
|
<person type="natural">
|
<name>Manoel</name>
|
<city>Palmas-TO</city>
|
</person>
|
<person type="legal">
|
<name>University of Brasília</name>
|
<city>Brasília-DF</city>
|
</person>
|
</people>
|
]]
|
|
--Instantiates the XML parser
|
local parser = xml2lua.parser(handler)
|
parser:parse(xml)
|
|
--Manually prints the table (since the XML structure for this example is previously known)
|
for i, p in pairs(handler.root.people.person) do
|
print(i, "Name:", p.name, "City:", p.city, "Type:", p._attr.type)
|
end
|
-- 递归处理嵌套表,合并 _attr 字段
|
local function process_table(t)
|
if type(t) ~= "table" then return t end
|
local result = {}
|
for k, v in pairs(t) do
|
if k == "_attr" then
|
-- 合并属性到外层对象
|
for attr_key, attr_val in pairs(v) do
|
result[attr_key] = attr_val
|
end
|
else
|
-- 递归处理子表
|
result[k] = process_table(v)
|
end
|
end
|
return result
|
end
|
local json = require("json")
|
-- 处理根表后再转换
|
local processed_root = process_table(handler.root)
|
local json_str = json.encode(processed_root)
|
print(json_str)
|
print(123)
|
|
local people = {
|
person = {
|
{name="Manoel", city="Palmas-TO", _attr={ type='natural' } },
|
{name="Breno", city="Palmas-TO", _attr={ type='legal' } }
|
}
|
}
|
|
print("People Table\n")
|
xml2lua.printable(people)
|
|
print()
|
print("XML Representation\n")
|
print(xml2lua.toXml(people, "people"))
|
pint(456)
|