This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

XML
#1
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>
Reply
#2
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
Reply


Forum Jump: