2

安装LuaXml

下载luaxml
然后修改Makefile

# 2009-03-16 by gf

# generic compiler and linker settings:
CC     = gcc
INCDIR = -I/usr/local/openresty/luajit/include/luajit-2.1
LIB    = 
LIBDIR =  -L. -L/usr/local/openresty/luajit/lib
CFLAGS = -Os -Wall -c #-g

# generic platform specific rules:
ARCH            = $(shell uname -s)
ifeq ($(ARCH),Linux)
  CFLAGS += -fPIC
  LFLAGS =  -fPIC -shared
  LIBS          = $(LIBDIR) $(LIB) -lluajit-5.1 -ldl
  EXESUFFIX =
  SHLIBSUFFIX = .so

else  
  ifeq ($(ARCH),Darwin) # MacOSX
    LFLAGS = -bundle 
    LIBS          = $(LIBDIR) -L/usr/local/lib $(LIB) -lluajit-5.1
    EXESUFFIX = .app
    SHLIBSUFFIX = .so
    
  else  # windows, MinGW
    LFLAGS =  -shared
    LIBS          = $(LIBDIR) $(LIB) -llua51 -mconsole -s
    EXESUFFIX = .exe
    SHLIBSUFFIX = .dll

  endif
endif

# project specific targets:
all:  LuaXML_lib$(SHLIBSUFFIX)

# project specific link rules:
LuaXML_lib$(SHLIBSUFFIX): LuaXML_lib.o
    $(CC) -o $@ $(LFLAGS) $^ $(LIBS) 

# project specific dependencies:
LuaXML_lib.o:  LuaXML_lib.c

# generic rules and targets:
.c.o:
    $(CC) $(CFLAGS) $(INCDIR) -c $<
clean:
    rm -f *.o *~ LuaXML_lib.so LuaXML_lib.dll

主要修改如下几个地方:

INCDIR = -I/usr/local/openresty/luajit/include/luajit-2.1
LIBDIR =  -L. -L/usr/local/openresty/luajit/lib
LIBS          = $(LIBDIR) $(LIB) -lluajit-5.1 -ldl
LIBS          = $(LIBDIR) -L/usr/local/lib $(LIB) -lluajit-5.1
LIBS          = $(LIBDIR) $(LIB) -llua51 -mconsole -s

拷贝到openresty/openresty:1.11.2.1-trusty镜像实例中,然后再此基础上编译

make clean && make

然后拷贝LuaXML_lib.so以及LuaXml.lua到/usr/local/openresty/lualib/

使用luaxml

    require "LuaXml"
    xml = require "xml"
    local resp = xml.eval(xmlStr)
    local oktag = resp:find("ok")
    local ok = oktag[1]
    ngx.log(ngx.INFO,"ok tag:"..ok)
  • 关于xml
    如果不require "xml",而是直接用xml的话,则第二次请求会报错

attempt to index global 'xml' (a nil value)    
  • 关于eval
    返回的是lua table,通过find出来,也是一个table,取出相应的值接口,如果是单值,取数组第一个,比如xmlStr是

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<result>
<ok>false</ok>
</result>

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论