1
Jianw
9 天以前 f6f5e6b632d6649386a380558d84003f3de7ec6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
-- 获取源文件信息
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)