![]() |
|
XML - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: XML (/showthread.php?tid=3043) |
XML - tomnord - 02.12.2020 Hi. How would one go about extracting data from an XML like this? Code: String: <?xml version=”1.0″ encoding=”UTF-8″?><sbn_rd xmlns=”http://www.ibsiberia.com/schemas/sbn_rd” version=”1″><event id=”1476139372″ protocol=”CID”><account>2034</account><code>E130</code><zone>1</zone><area>1</area></event></sbn_rd>RE: XML - admin - 03.12.2020 Use this: Code: function starttag(p, tag, attrs)
if tag == 'event' then
eventdata = attrs
elseif not done and eventdata then
ctag = tag
end
end
function endtag(p, tag)
if tag == 'event' then
done = true
elseif tag == ctag then
ctag = nil
end
end
function text(p, tdata)
if ctag then
eventdata[ ctag ] = tdata:trim()
end
end
data = '<?xml version="1.0" encoding="UTF-8"?><sbn_rd xmlns="http://www.ibsiberia.com/schemas/sbn_rd" version="1"><event id="1476139372" protocol="CID"><account>2034</account><code>E130</code><zone>1</zone><area>1</area></event></sbn_rd>'
require('lxp').new({
StartElement = starttag,
EndElement = endtag,
CharacterData = text,
}):parse(data)
log(eventdata)It will produce a table containing all the event data: Code: * table:
[1]
* string: id
[2]
* string: protocol
["area"]
* string: 1
["code"]
* string: E130
["account"]
* string: 2034
["protocol"]
* string: CID
["id"]
* string: 1476139372
["zone"]
* string: 1 |