public
Description: A Lua implementation of the XmlReader API using libxml2
Homepage: http://asbradbury.org/projects/lua-xmlreader/
Clone URL: git://github.com/asb/lua-xmlreader.git
lua-xmlreader / example.lua
100755 31 lines (27 sloc) 0.739 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#! /usr/bin/env lua
-- See http://xmlsoft.org/xmlreader.html for more examples
require("xmlreader")
 
local example_xml = [[
<?xml version="1.0" encoding="iso-8859-1" ?>
<library>
<book id='1'>
<title>Green Eggs and Ham</title>
<author>Dr. Seuss</author>
</book>
<book id='2'>
<title>Where the Wild Things Are</title>
<author>Maurice Sendak</author>
</book>
</library>
]]
 
local r = assert(xmlreader.from_string(example_xml))
 
while (r:read()) do
  local leadingws = (' '):rep(r:depth())
  if (r:node_type() == "element") then
    io.write(("%s%s:"):format(leadingws, r:name()))
    while (r:move_to_next_attribute()) do
      io.write((' %s=%q'):format(r:name(), r:value()))
    end
    io.write('\n')
  end
end