0

Good afternoon everyone,

My problem is that I have 2 XML lists
<List1> <Agency>String</Agency> </List1>
and
<List2><Agency2>String</Agency2><List2>.
In Lua I need to create a program which is parsing this list and when the user inputs a matching string from List 1 or List 2, the program needs to actually confirm to the user if the string belongs to either L1 or L2 or if the string is inexistent. I'm new to Lua and to programming generally speaking and I would be very grateful for you answers. I have LuaExpat as a plugin but I can't seem to be able to actually read from file, I can only do some beginner tricks if the xml list is written in the code. At a later time this small program will be fed by an RSS.

Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23
  • what's wrong with the Getting Started resources listed on the Lua website? The cover everything you need to know. Don't know how to read a file? Read the manual, do a tutorial. Don't know how to find a string in another string? Read the manual, do a tutorial. It all boils down to reading a file into a string and searching that string for another string. Lua's io and string standard libraries provide all means you can wish for. You just have to learn the very basics of the language and break your problem down into atomic sub-problems. that's not our job. – Piglet Feb 21 '18 at 12:12
  • @Piglet - The question is about using LuaExpat, not standard Lua libraries. – Egor Skriptunoff Feb 21 '18 at 14:58
  • @AlexandruIvan - You should show us what you have tried, example of your code. What exactly is unclear to you about reading XML through LuaExpat? Does manual really unhelpful in your task? Piglet showed such emotional reaction because you actually asked "code for me" instead of describing your problem and showing your attempts to solve it yourself. Actually, SO is not "write a code for me" site. – Egor Skriptunoff Feb 21 '18 at 15:04
  • Get some manners. I simply pointed out that your problem can be solved with Lua's standard libraries and that you should do a tutorial as you mentioned that you are new to Lua... your attitude will definitely not get you any further in life. But go ahead, keep on insulting people who try to help you and die ignorant. – Piglet Feb 22 '18 at 07:26
  • @EgorSkriptunoff : require"lom" lom=lxp.lom handle= io.open("Agencies.xml", "r") handle2 = io.open("Agencies1.xml", "r") tbl= lom.parse(handle:read("*a")) tbl1= lom.parse(handle2:read("*a")) handle:close() Now, if I'm doing the XML table inside the code as "spaghetti code" I can browse to it, but I can't browse through the files, this is my only problem. As I'm applying LOM I can't seem to browse through the XML indexes. – Alexandru Ivan Feb 22 '18 at 08:02
  • @EgorSkriptunoff : No reaction is a better reaction. He made sure that my first experience w stackoverflow, my first post, was treated w arrogance :) Even if it was a "code for me" request, he could've said " Ok, this is a "code for me request, I'll do it for whatever cash payment instead of telling me that " it's not our job ", that was hilarious! I am just starting with programing and I'm doing so in a language which practically does not exist in this day and age, I have no logic into setting things up and I need help. Ppl like Piglet don't know how to help or advice people :) I'm sorry. – Alexandru Ivan Feb 22 '18 at 08:14
  • @EgorSkriptunoff... https://community.qlik.com/docs/DOC-3691 ... this is my refference tutorial BUT when I try to print any index it returns a NIL value... – Alexandru Ivan Feb 22 '18 at 08:47
  • require("lxp") local stuff = {} xmldata=" " function doFunc(parser, name, attr) if not (name == 'B') then return end stuff[#stuff+1]= attr end local xml = lxp.new{StartElement = doFunc} xml:parse(xmldata) xml:close() print(stuff[2].a) It works like this, but I don't know how to do this from file... – Alexandru Ivan Feb 22 '18 at 14:48
  • @AlexandruIvan - Please add your code to your question instead of comments. And apply proper formatting to make source code easily readable - this will make other people more likely to help you. – Egor Skriptunoff Feb 22 '18 at 16:09

1 Answers1

0
require("lxp") 
local stuff = {} 

xmldata="<Top><A/> <B a='1'/> <B a='2'/><B a='3'/><C a='3'/></Top>" 

function doFunc(parser, name, attr) 
  if not (name == 'B') then return end 
  stuff[#stuff+1]= attr 
end 

local xml = lxp.new{StartElement = doFunc} 
xml:parse(xmldata) 
xml:close() 

print(stuff[3].a) 

This code is a tutorial over the web that works, everything is just fine it prints nr. 3. Now I want to know how to do that from an actual file, as if I input io.read:(file, "r" or "rb" ) under xmldata variable and run the same thing it returns either empty space or nil.