Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1489617
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: 项目管理

2015-01-06 10:57:51

1. luaxml的官网     对应的有lua5.1和lua5.2的版本

2. 核心代码有2个, 一个LuaXML_lib.C文件, 一个LuaXML.lua文件

点击(此处)折叠或打开

  1. /**
  2. LuaXML License

  3. LuaXml is licensed under the terms of the MIT license reproduced below,
  4. the same as Lua itself. This means that LuaXml is free software and can be
  5. used for both academic and commercial purposes at absolutely no cost.

  6. Copyright (C) 2007-2010 Gerald Franz, www.viremo.de

  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:

  13. http://viremo.eludi.net/LuaXML/

  14. */

  15. #if defined __WIN32__ || defined WIN32
  16. # include <windows.h>
  17. # define _EXPORT __declspec(dllexport)
  18. #else
  19. # define _EXPORT
  20. #endif

  21. #include <lua.h>
  22. #include <lauxlib.h>
  23. #include <lualib.h>

  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <stdlib.h>

  28. static const char ESC=27;
  29. static const char OPN=28;
  30. static const char CLS=29;

  31. //--- auxliary functions -------------------------------------------

  32. static const char* char2code(unsigned char ch, char buf[8]) {
  33.     unsigned char i=0;
  34.     buf[i++]='&';
  35.     buf[i++]='#';
  36.     if(ch>99) buf[i++]=ch/100+48;
  37.     if(ch>9) buf[i++]=(ch%100)/10+48;
  38.     buf[i++]=ch%10+48;
  39.     buf[i++]=';';
  40.     buf[i]=0;
  41.     return buf;
  42. }

  43. static size_t find(const char* s, const char* pattern, size_t start) {
  44.     const char* found =strstr(s+start, pattern);
  45.     return found ? found-s : strlen(s);
  46. }

  47. //--- internal tokenizer -------------------------------------------

  48. typedef struct Tokenizer_s {
  49.     const char* s;                        // stores string to be tokenized
  50.     size_t s_size;                        // stores size of string to be tokenized
  51.     size_t i;                                    // stores current read position
  52.     int tagMode;                            // stores current read context
  53.     const char* m_next;                // stores next token, if already determined
  54.     size_t m_next_size;                // size of next token
  55.     char* m_token;                        // pointer to current token
  56.     size_t m_token_size;            // size of current token
  57.     size_t m_token_capacity;    // capacity of current token
  58. } Tokenizer;

  59. Tokenizer* Tokenizer_new(const char* str, size_t str_size) {
  60.     Tokenizer *tok = (Tokenizer*)malloc(sizeof(Tokenizer));
  61.     memset(tok, 0, sizeof(Tokenizer));
  62.     tok->s_size = str_size;
  63.     tok->s = str;
  64.     return tok;
  65. }

  66. void Tokenizer_delete(Tokenizer* tok) {
  67.     free(tok->m_token);
  68.     free(tok);
  69. }

  70. //void Tokenizer_print(Tokenizer* tok) { printf(" @%u %s\n", tok->i, !tok->m_token ? "(null)" : (tok->m_token[0]==ESC)?"(esc)" : (tok->m_token[0]==OPN)?"(open)": (tok->m_token[0]==CLS)?"(close)" : tok->m_token); fflush(stdout); }
  71. static const char* Tokenizer_set(Tokenizer* tok, const char* s, size_t size) {
  72.     if(!size||!s) return 0;
  73.     free(tok->m_token);
  74.     tok->m_token = (char*)malloc(size+1);
  75.     strncpy(tok->m_token,s, size);
  76.     tok->m_token[size] = 0;
  77.     tok->m_token_size = tok->m_token_capacity = size;
  78.     //Tokenizer_print(tok);
  79.     return tok->m_token;
  80. }

  81. static void Tokenizer_append(Tokenizer* tok, char ch) {
  82.     if(tok->m_token_size+1>=tok->m_token_capacity) {
  83.         tok->m_token_capacity = (tok->m_token_capacity==0) ? 16 : tok->m_token_capacity*2;
  84.         tok->m_token = (char*)realloc(tok->m_token, tok->m_token_capacity);
  85.     }
  86.     tok->m_token[tok->m_token_size]=ch;
  87.     tok->m_token[++tok->m_token_size]=0;
  88. }

  89. const char* Tokenizer_next(Tokenizer* tok) {
  90.     const char* ESC_str = "\033";
  91.     const char* OPEN_str = "\034";
  92.     const char* CLOSE_str = "\035";


  93.     if(tok->m_token) {
  94.         free(tok->m_token);
  95.         tok->m_token = 0;
  96.         tok->m_token_size=tok->m_token_capacity = 0;
  97.     }

  98.     int quotMode=0;
  99.     int tokenComplete = 0;
  100.     while(tok->m_next_size || (tok->i < tok->s_size)) {

  101.         if(tok->m_next_size) {
  102.             Tokenizer_set(tok, tok->m_next, tok->m_next_size);
  103.             tok->m_next=0;
  104.             tok->m_next_size=0;
  105.             return tok->m_token;
  106.         }

  107.         switch(tok->s[tok->i]) {
  108.          case '"':
  109.          case '\'':
  110.             if(tok->tagMode) {
  111.                 if(!quotMode) quotMode=tok->s[tok->i];
  112.                 else if(quotMode==tok->s[tok->i]) quotMode=0;
  113.             }
  114.             Tokenizer_append(tok, tok->s[tok->i]);
  115.             break;
  116.          case '<':
  117.             if(!quotMode&&(tok->i+4s_size)&&(strncmp(tok->s+tok->i,"<!--",4)==0)) // strip comments
  118.              tok->i=find(tok->s, "-->", tok->i+4)+2;
  119.             else if(!quotMode&&(tok->i+9s_size)&&(strncmp(tok->s+tok->i,"<![CDATA[",9)==0)) { // interpet CDATA
  120.              size_t b=tok->i+9;
  121.              tok->i=find(tok->s, "]]>",b)+3;
  122.              if(!tok->m_token_size) return Tokenizer_set(tok, tok->s+b, tok->i-b-3);
  123.              tokenComplete = 1;
  124.              tok->m_next = tok->s+b;
  125.              tok->m_next_size = tok->i-b-3;
  126.              --tok->i;
  127.             }
  128.             else if(!quotMode&&(tok->i+1s_size)&&((tok->s[tok->i+1]=='?')||(tok->s[tok->i+1]=='!'))) // strip meta information
  129.              tok->i=find(tok->s, ">", tok->i+2);
  130.             else if(!quotMode&&!tok->tagMode) {
  131.                 if((tok->i+1s_size)&&(tok->s[tok->i+1]=='/')) {
  132.                     tok->m_next=ESC_str;
  133.                     tok->m_next_size = 1;
  134.                     tok->i=find(tok->s, ">", tok->i+2);
  135.                 }
  136.                 else {
  137.                     tok->m_next = OPEN_str;
  138.                     tok->m_next_size = 1;
  139.                     tok->tagMode=1;
  140.                 }
  141.                 tokenComplete = 1;
  142.             }
  143.             else Tokenizer_append(tok, tok->s[tok->i]);
  144.             break;
  145.          case '/':
  146.             if(tok->tagMode&&!quotMode) {
  147.                 tokenComplete = 1;
  148.                 if((tok->i+1 < tok->s_size) && (tok->s[tok->i+1]=='>')) {
  149.                     tok->tagMode=0;
  150.                     tok->m_next=ESC_str;
  151.                     tok->m_next_size = 1;
  152.                     ++tok->i;
  153.                 }
  154.                 else Tokenizer_append(tok, tok->s[tok->i]);
  155.             }
  156.             else Tokenizer_append(tok, tok->s[tok->i]);
  157.             break;
  158.          case '>':
  159.             if(!quotMode&&tok->tagMode) {
  160.                 tok->tagMode=0;
  161.                 tokenComplete = 1;
  162.                 tok->m_next = CLOSE_str;
  163.                 tok->m_next_size = 1;
  164.             }
  165.             else Tokenizer_append(tok, tok->s[tok->i]);
  166.             break;
  167.          case ' ':
  168.          case '\r':
  169.          case '\n':
  170.          case '\t':
  171.             if(tok->tagMode&&!quotMode) {
  172.              if(tok->m_token_size) tokenComplete=1;
  173.             }
  174.             else if(tok->m_token_size) Tokenizer_append(tok, tok->s[tok->i]);
  175.             break;
  176.          default: Tokenizer_append(tok, tok->s[tok->i]);
  177.         }
  178.         ++tok->i;
  179.         if((tok->i>=tok->s_size)||(tokenComplete&&tok->m_token_size)) {
  180.             tokenComplete=0;
  181.             while(tok->m_token_size&&isspace(tok->m_token[tok->m_token_size-1])) // trim whitespace
  182.                 tok->m_token[--tok->m_token_size]=0;
  183.             if(tok->m_token_size) break;
  184.         }
  185.     }
  186.     //Tokenizer_print(tok);
  187.     return tok->m_token;
  188. }

  189. //--- local variables ----------------------------------------------

  190. /// stores number of special character codes
  191. static size_t sv_code_size=0;
  192. /// stores currently allocated capacity for special character codes
  193. static size_t sv_code_capacity=16;
  194. /// stores code table for special characters
  195. static char** sv_code=0;

  196. //--- public methods -----------------------------------------------

  197. static void Xml_pushDecode(lua_State* L, const char* s, size_t s_size) {
  198.     size_t start=0, pos;
  199.     if(!s_size) s_size=strlen(s);
  200.     luaL_Buffer b;
  201.     luaL_buffinit(L, &b);
  202.     const char* found = strstr(s, "&#");
  203.     if(!found) pos = s_size;
  204.     else pos = found-s;
  205.     while(found&&(pos+5
  206.         if(pos>start) luaL_addlstring(&b,s+start, pos-start);
  207.         luaL_addchar(&b, 100*(s[pos+2]-48)+10*(s[pos+3]-48)+(s[pos+4]-48));
  208.         start=pos+6;
  209.         found = strstr(found+6, "&#");
  210.         if(!found) pos = s_size;
  211.         else pos = found-s;
  212.     }
  213.     if(pos>start) luaL_addlstring(&b,s+start, pos-start);
  214.     luaL_pushresult(&b);
  215.     size_t i;
  216.     for(i=sv_code_size-1; i
  217.         luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i-1]);
  218.         lua_remove(L,-2);
  219.     }
  220. }

  221. int Xml_eval(lua_State *L) {
  222.     char* str = 0;
  223.     size_t str_size=0;
  224.     if(lua_isuserdata(L,1)) str = (char*)lua_touserdata(L,1);
  225.     else {
  226.         const char * sTmp = luaL_checklstring(L,1,&str_size);
  227.         str = (char*)malloc(str_size+1);
  228.         memcpy(str, sTmp, str_size);
  229.         str[str_size]=0;
  230.     }
  231.     Tokenizer* tok = Tokenizer_new(str, str_size ? str_size : strlen(str));
  232.     lua_settop(L,0);
  233.     const char* token=0;
  234.     int firstStatement = 1;
  235.     while((token=Tokenizer_next(tok))!=0) if(token[0]==OPN) { // new tag found
  236.         if(lua_gettop(L)) {
  237.             int newIndex=lua_objlen(L,-1)+1;
  238.             lua_pushnumber(L,newIndex);
  239.             lua_newtable(L);
  240.             lua_settable(L, -3);
  241.             lua_pushnumber(L,newIndex);
  242.             lua_gettable(L,-2);
  243.         }
  244.         else {
  245.             if (firstStatement) {
  246.                 lua_newtable(L);
  247.                 firstStatement = 0;
  248.             }
  249.             else return lua_gettop(L);
  250.         }
  251.         // set metatable:
  252.         lua_newtable(L);
  253.         lua_pushliteral(L, "__index");
  254.         lua_getglobal(L, "xml");
  255.         lua_settable(L, -3);

  256.         lua_pushliteral(L, "__tostring"); // set __tostring metamethod
  257.         lua_getglobal(L, "xml");
  258.         lua_pushliteral(L,"str");
  259.         lua_gettable(L, -2);
  260.         lua_remove(L, -2);
  261.         lua_settable(L, -3);
  262.         lua_setmetatable(L, -2);

  263.         // parse tag and content:
  264.         lua_pushnumber(L,0); // use index 0 for storing the tag
  265.         lua_pushstring(L, Tokenizer_next(tok));
  266.         lua_settable(L, -3);

  267.         while(((token = Tokenizer_next(tok))!=0)&&(token[0]!=CLS)&&(token[0]!=ESC)) { // parse tag header
  268.             size_t sepPos=find(token, "=", 0);
  269.             if(token[sepPos]) { // regular attribute
  270.                 const char* aVal =token+sepPos+2;
  271.                 lua_pushlstring(L, token, sepPos);
  272.                 size_t lenVal = strlen(aVal)-1;
  273.                 if(!lenVal) Xml_pushDecode(L, "", 0);
  274.                 else Xml_pushDecode(L, aVal, lenVal);
  275.                 lua_settable(L, -3);
  276.             }
  277.         }
  278.         if(!token||(token[0]==ESC)) {
  279.             if(lua_gettop(L)>1) lua_settop(L,-2); // this tag has no content, only attributes
  280.             else break;
  281.         }
  282.     }
  283.     else if(token[0]==ESC) { // previous tag is over
  284.         if(lua_gettop(L)>1) lua_settop(L,-2); // pop current table
  285.         else break;
  286.     }
  287.     else { // read elements
  288.         lua_pushnumber(L,lua_objlen(L,-1)+1);
  289.         Xml_pushDecode(L, token, 0);
  290.         lua_settable(L, -3);
  291.     }
  292.     Tokenizer_delete(tok);
  293.     free(str);
  294.     return lua_gettop(L);
  295. }

  296. int Xml_load (lua_State *L) {
  297.     const char * filename = luaL_checkstring(L,1);
  298.     FILE * file=fopen(filename,"r");
  299.     if(!file) return luaL_error(L,"LuaXml ERROR: \"%s\" file error or file not found!",filename);

  300.     fseek (file , 0 , SEEK_END);
  301.     size_t sz = ftell (file);
  302.     rewind (file);
  303.     char* buffer = (char*)malloc(sz+1);
  304.     sz = fread (buffer,1,sz,file);
  305.     fclose(file);
  306.     buffer[sz]=0;
  307.     lua_pushlightuserdata(L,buffer);
  308.     lua_replace(L,1);
  309.     return Xml_eval(L);
  310. };

  311. int Xml_registerCode(lua_State *L) {
  312.     const char * decoded = luaL_checkstring(L,1);
  313.     const char * encoded = luaL_checkstring(L,2);

  314.     size_t i;
  315.     for(i=0; i
  316.         if(strcmp(sv_code[i],decoded)==0)
  317.             return luaL_error(L,"LuaXml ERROR: code already exists.");
  318.     if(sv_code_size+2>sv_code_capacity) {
  319.         sv_code_capacity*=2;
  320.         sv_code = (char**)realloc(sv_code, sv_code_capacity*sizeof(char*));
  321.     }
  322.     sv_code[sv_code_size]=(char*)malloc(strlen(decoded)+1);
  323.     strcpy(sv_code[sv_code_size++], decoded);
  324.     sv_code[sv_code_size]=(char*)malloc(strlen(encoded)+1);
  325.     strcpy(sv_code[sv_code_size++],encoded);
  326.     return 0;
  327. }

  328. int Xml_encode(lua_State *L) {
  329.     if(lua_gettop(L)!=1) return 0;
  330.     luaL_checkstring(L,-1);
  331.     size_t i;
  332.     for(i=0; i
  333.         luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i+1]);
  334.         lua_remove(L,-2);
  335.     }
  336.     char buf[8];
  337.     const char* s=lua_tostring(L,1);
  338.     size_t start, pos;
  339.     luaL_Buffer b;
  340.     luaL_buffinit(L, &b);
  341.     for(start=pos=0; s[pos]!=0; ++pos) if(s[pos]<0) {
  342.         if(pos>start) luaL_addlstring(&b,s+start, pos-start);
  343.         luaL_addstring(&b,char2code((unsigned char)(s[pos]),buf));
  344.         start=pos+1;
  345.     }
  346.     if(pos>start) luaL_addlstring(&b,s+start, pos-start);
  347.     luaL_pushresult(&b);
  348.     lua_remove(L,-2);
  349.     return 1;
  350. }

  351. int _EXPORT luaopen_LuaXML_lib (lua_State* L) {
  352.     static const struct luaL_Reg funcs[] = {
  353.         {"load", Xml_load},
  354.         {"eval", Xml_eval},
  355.         {"encode", Xml_encode},
  356.         {"registerCode", Xml_registerCode},
  357.         {NULL, NULL}
  358.     };
  359.     luaL_register(L, "xml", funcs);
  360.     // register default codes:
  361.     if(!sv_code) {
  362.         sv_code=(char**)malloc(sv_code_capacity*sizeof(char*));
  363.         sv_code[sv_code_size++]="&";
  364.         sv_code[sv_code_size++]="&amp;";
  365.         sv_code[sv_code_size++]="<";
  366.         sv_code[sv_code_size++]="&lt;";
  367.         sv_code[sv_code_size++]=">";
  368.         sv_code[sv_code_size++]="&gt;";
  369.         sv_code[sv_code_size++]="\"";
  370.         sv_code[sv_code_size++]=""";
  371.         sv_code[sv_code_size++]="'";
  372.         sv_code[sv_code_size++]="'";
  373.     }
  374.     return 1;
  375. }

点击(此处)折叠或打开

  1. --[[ LuaXML.lua的核心代码
  2. http://viremo.eludi.net/LuaXML/
  3. version 1.8.0 (Lua 5.2), LuaXML_130610.zip
  4. version 1.7.4 (Lua 5.1), LuaXML_101012.zip

  5. xml.new(arg):                     创建一个新的XML对象
  6. xml.append(var, tag):     添加一个子节点
  7. xml.load(filename):         加载XML文件
  8. xml.save(var,filename): 保存XML文件
  9. xml.eval(xmlstring):         解析XML字符串
  10. xml.tag(var, tag):             设置或返回一个XML对象
  11. xml.str(var, indent, tag): 以字符串形式返回XML
  12. xml.find(var, tag, attributeKey, attributeValue): 查找子节点
  13. xml.registerCode(decoded, encoded): 设置文件编码类型

  14. ]]--

  15. require("LuaXML_lib")

  16. local base = _G
  17. local xml = xml
  18. module("xml")

  19. -- symbolic name for tag index, this allows accessing the tag by var[xml.TAG]
  20. TAG = 0

  21. -- sets or returns tag of a LuaXML object
  22. function tag(var,tag)
  23.   if base.type(var)~="table" then return end
  24.   if base.type(tag)=="nil" then
  25.     return var[TAG]
  26.   end
  27.   var[TAG] = tag
  28. end

  29. -- creates a new LuaXML object either by setting the metatable of an existing Lua table or by setting its tag
  30. function new(arg)
  31.   if base.type(arg)=="table" then
  32.     base.setmetatable(arg,{__index=xml, __tostring=xml.str})
  33.     return arg
  34.   end
  35.   local var={}
  36.   base.setmetatable(var,{__index=xml, __tostring=xml.str})
  37.   if base.type(arg)=="string" then var[TAG]=arg end
  38.   return var
  39. end

  40. -- appends a new subordinate LuaXML object to an existing one, optionally sets tag
  41. function append(var,tag)
  42.   if base.type(var)~="table" then return end
  43.   local newVar = new(tag)
  44.   var[#var+1] = newVar
  45.   return newVar
  46. end

  47. -- converts any Lua var into an XML string
  48. function str(var,indent,tagValue)
  49.   if base.type(var)=="nil" then return end
  50.   local indent = indent or 0
  51.   local indentStr=""
  52.   for i = 1,indent do indentStr=indentStr.." " end
  53.   local tableStr=""

  54.   if base.type(var)=="table" then
  55.     local tag = var[0] or tagValue or base.type(var)
  56.     local s = indentStr.."<"..tag
  57.     for k,v in base.pairs(var) do -- attributes
  58.       if base.type(k)=="string" then
  59.         if base.type(v)=="table" and k~="_M" then -- otherwise recursiveness imminent
  60.           tableStr = tableStr..str(v,indent+1,k)
  61.         else
  62.           s = s.." "..k.."=\""..encode(base.tostring(v)).."\""
  63.         end
  64.       end
  65.     end
  66.     if #var==0 and #tableStr==0 then
  67.       s = s.." />\n"
  68.     elseif #var==1 and base.type(var[1])~="table" and #tableStr==0 then -- single element
  69.       s = s..">"..encode(base.tostring(var[1])).."..tag..">\n"
  70.     else
  71.       s = s..">\n"
  72.       for k,v in base.ipairs(var) do -- elements
  73.         if base.type(v)=="string" then
  74.           s = s..indentStr.." "..encode(v).." \n"
  75.         else
  76.           s = s..str(v,indent+1)
  77.         end
  78.       end
  79.       s=s..tableStr..indentStr.."..tag..">\n"
  80.     end
  81.     return s
  82.   else
  83.     local tag = base.type(var)
  84.     return indentStr.."<"..tag.."> "..encode(base.tostring(var)).." ..tag..">\n"
  85.   end
  86. end


  87. -- saves a Lua var as xml file
  88. function save(var,filename)
  89.   if not var then return end
  90.   if not filename or #filename==0 then return end
  91.   local file = base.io.open(filename,"w")
  92.   file:write("\n\n\n")
  93.   file:write(str(var))
  94.   base.io.close(file)
  95. end


  96. -- recursively parses a Lua table for a substatement fitting to the provided tag and attribute
  97. function find(var, tag, attributeKey,attributeValue)
  98.   -- check input:
  99.   if base.type(var)~="table" then return end
  100.   if base.type(tag)=="string" and #tag==0 then tag=nil end
  101.   if base.type(attributeKey)~="string" or #attributeKey==0 then attributeKey=nil end
  102.   if base.type(attributeValue)=="string" and #attributeValue==0 then attributeValue=nil end
  103.   -- compare this table:
  104.   if tag~=nil then
  105.     if var[0]==tag and ( attributeValue == nil or var[attributeKey]==attributeValue ) then
  106.       base.setmetatable(var,{__index=xml, __tostring=xml.str})
  107.       return var
  108.     end
  109.   else
  110.     if attributeValue == nil or var[attributeKey]==attributeValue then
  111.       base.setmetatable(var,{__index=xml, __tostring=xml.str})
  112.       return var
  113.     end
  114.   end
  115.   -- recursively parse subtags:
  116.   for k,v in base.ipairs(var) do
  117.     if base.type(v)=="table" then
  118.       local ret = find(v, tag, attributeKey,attributeValue)
  119.       if ret ~= nil then return ret end
  120.     end
  121.   end
  122. end


重新写个Makefile, 生成新环境下的so文件

点击(此处)折叠或打开

  1. OPENWRT = 1

  2. ifeq ($(OPENWRT), 1)
  3.     #CC = ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
  4.     CC = ~/OpenWrt-Toolchain-ar71xx-for-mips_r2-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
  5.     CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib

  6. else
  7.     CC = gcc
  8. endif

  9. CFLAGS += -Wall -O2
  10. #CFLAGS += -g
  11. CFLAGS1 += -fPIC -shared        #仅so 文件需要

  12. #可执行文件名和相关的源码文件. lua的so和bin无法同时生成.
  13. SHAREOBJ = LuaXML_lib.so
  14. SRCS += LuaXML_lib.c

  15. #以下无需改动
  16. OBJS = $(SRCS:.c=.o)

  17. all: SO_FILE

  18. SO_FILE: $(OBJS)
  19.     $(CC) $(CFLAGS) $(CFLAGS1) $(SRCS) -o $(SHAREOBJ) $(LFLAGS1)

  20. .PHONY: clean
  21. clean:
  22.     @echo "cleanning project"
  23.     $(RM) *.a $(OBJS) *~ *.so *.lo $(APP_BINARY) $(SHAREOBJ)
  24.     @echo "clean completed"

将此新生成的LuaXML_lib.so和LuaXML.lua拷贝到路由器上的 /usr/lib/lua目录下即可. 


3. openwrt下的测试

点击(此处)折叠或打开

  1. require('LuaXml')

  2. -- 加载XML文件
  3. local xfile = xml.load("test1.xml")
  4. -- 查找子节点
  5. local item = xfile:find("item")
  6. -- 节点不为空
  7. if item ~= nil then
  8.  print(item);
  9.  -- 获得子节点数量
  10.  print( "num = " .. table.getn(item))

  11.  -- 修改键值
  12.  print("old id = " .. item.id)
  13.  item.id = "abc";
  14.  print("new id = " .. item.id)

  15.  -- 第1个子节点
  16.  local field = item[1]
  17.  print(field.name, field)

  18.  -- 第2个子节点
  19.  field = item[2]
  20.  print(field.name, field)
  21. end

  22. -- 添加子节点
  23. local xNewFile = xml.new("root")
  24. -- 设置子节点键值
  25. local child = xNewFile:append("child")
  26. child.id = 1
  27. xNewFile:append("text1")[1] = 'test1'

  28. xNewFile:append("child").id = 2
  29. xNewFile:append("text2")[1] = 'test2'
  30. --print( xNewFile)
  31. -- 保存文件
  32. xNewFile:save "t1.xml"

  33. -- 构造xml字符串
  34. local str_xml = xml.str(xNewFile)
  35. --local str_xml = " test1 test2"
  36. --local str_xml = " test12 test21 "
  37. print("xml string: \n" .. str_xml)

  38. -- 解析xml字符串
  39. local xml_info = xml.eval(str_xml)

  40. local field_name = "text1"
  41. local item = xml_info:find(field_name)
  42. if item ~= nil then
  43.     local value = item[1]
  44.     print(field_name .. " = " .. value)
  45. end

  46. local field_name = "text2"
  47. local item = xml_info:find(field_name)
  48. if item ~= nil then
  49.     local value = item[1]
  50.     print(field_name .. " = " .. value)
  51. end

运行结果如下

点击(此处)折叠或打开

  1. root@OpenWrt:/xutest/lua# lua test1.lua
  2. <item id="1">
  3.   <field name="aa" />
  4.   <field name="bb" />
  5. </item>

  6. num = 2
  7. old id = 1
  8. new id = abc
  9. aa <field name="aa" />

  10. bb <field name="bb" />

  11. xml string:
  12. <root>
  13.   <child id="1" />
  14.   <text1>test1</text1>
  15.   <child id="2" />
  16.   <text2>test2</text2>
  17. </root>

  18. text1 = test1
  19. text2 = test2
  20. root@OpenWrt:/xutest/lua

阅读(2817) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~