summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luaxml/dom-sample.lua
blob: d3fcde51ff0370faff4adc67417b7fe1e1ab9c3b (plain)
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
--kpse.set_program_name("luatex")
function traverseDom(parser, current,level)
	local level = level or 0
        local spaces = string.rep(" ",level)
	local root= current or parser._handler.root
	local name = root._name or "unnamed"
	local xtype = root._type or "untyped"
	local attributes = root._attr  or {} 
	if xtype == "TEXT" then 
		print(spaces .."TEXT : " .. root._text)
	else	 
		print(spaces .. xtype .. " : " .. name) 
	end
	for k, v in pairs(attributes) do
		print(spaces .. "  ".. k.."="..v)
	end
	local children = root._children or {}
	for _, child in ipairs(children) do
		traverseDom(parser,child, level + 1)
	end
end

local xml = require('luaxml-mod-xml')
local handler = require('luaxml-mod-handler')
local x = '<p>hello <a href="http://world.com/">world</a>, how are you?</p>'
local domHandler = handler.domHandler()
local parser = xml.xmlParser(domHandler)
parser:parse(x)
traverseDom(parser)