-- 获取源文件信息 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 cjson = require("json") -- SOAP XML 数据 local soap_xml = [[ SIMPLIFIED CHINESE 0 CBJJT1 北京京泰日晟科技有限公司 CBJJXK 北京吉鑫康商贸有限公司 ]] soap_xml = [[ SIMPLIFIED CHINESE 0 KH32803017 CGKHTY 螺旋刀片式髓内钉 螺旋刀片式股骨近端髓内钉10×170 JGDⅥ φ10×170 1 1 2 3 1 1 0 A KH-创伤 常温 small 50 ]] -- 解析配置 local parser = xml2lua.parser(handler) -- parser:parse(soap_xml) -- 注意冒号调用[7](@ref) -- 数据清洗函数(处理命名空间和空值) local function clean_data(data) local function process(tbl) local result = {} for k, v in pairs(tbl) do -- 去除命名空间前缀[6,9](@ref) local new_key = k:gsub("v1:", ""):gsub("soap:", "") if type(v) == "table" then -- 处理数组结构[4](@ref) if #v > 0 then local arr = {} for i, item in ipairs(v) do arr[i] = process(item) end result[new_key] = arr else result[new_key] = process(v) end else result[new_key] = v ~= "" and v or nil end end return result end return process(handler.root) end -- 生成 JSON -- local cleaned = clean_data(handler.root) -- local json_str = cjson.encode(cleaned) -- 使用 cjson 序列化[3,4](@ref) print(json_str) -- 安全解析函数 local function safe_parse(xml_str) local ok, err = pcall(function() parser:parse(xml_str) -- 使用 pcall 捕获异常[7](@ref) end) if not ok then print("XML解析失败:", err) return nil end return handler.root end local cleaned = nil; -- 执行解析 local parsed_data = safe_parse(soap_xml) if parsed_data then cleaned = clean_data(parsed_data) print(cjson.encode(cleaned)) end -- 查找 Req 元素(body 下的第一个元素) -- 获取 soapenv:Body 下的所有键 local body_keys = {} local body = cleaned["soapenv:Envelope"]["soapenv:Body"] for k, _ in pairs(body) do table.insert(body_keys, k) end -- 使用数字索引 1 获取 Req 元素(Lua 索引从 1 开始) local req_key = body_keys[1] local req = body[req_key] -- 获取 Req 下的所有键 local req_keys = {} for k, _ in pairs(req) do table.insert(req_keys, k) end -- 使用数字索引 2 获取 Input 元素 local input_key = req_keys[2] local input = req[input_key] -- 提取 InputParameters 数据 local input_parameters = input["InputParameters"] -- 打印 InputParameters 数据 local function print_table(t, indent) indent = indent or 0 for k, v in pairs(t) do local prefix = string.rep(" ", indent) if type(v) == "table" then print(prefix .. tostring(k) .. ":") print_table(v, indent + 1) else print(prefix .. tostring(k) .. ": " .. tostring(v)) end end end print_table(input_parameters)