lure.tuts.parsepreview

Parser Preview

The parser in its current state implements many of the core DOM node objects. Although the library is not yet ready for prototype release, below is an example of the current xml/html parser and some basic use of the resulting DOM Object.

Below is very simple xml/html markup:

<html>
	<head>
		<title>Lure Test Page</title>		
	</head>
	<body>
		<div id="div1">
			this is a sentence<br>separated by a break line.
			<div id="div2">Hello</div>
			<div id="div3">World</div>			
		</div>
	</body>
</html>

And to parse and work with the DOM in LURE:

require("lure.lua")

htmlDoc = Lure.load("test.html")
print(htmlDoc.getElementById("div1").childNodes[1].data)
--OUTPUT: this is a sentence

print(htmlDoc.childNodes[1].childNodes[2].childNodes[1].childNodes[3].data)
--OUTPUT: separated by a break line.

newElement = htmlDoc.createElement("div")
newElement.setAttribute("id", "myNewElement")
htmlDoc.childNodes[1].childNodes[2].appendChild(newElement)
print(htmlDoc.getElementById("myNewElement").getAttribute("id"))
--OUTPUT: myNewElement

Since the parser is non-validating (no docTypes, namespaces or other shenanigans), we can parse and produce a DOM object using arbitrary xml structures. Lets represent a list of players using XML:

<players>
	<player id="p1">
                <desc>I am player 1</desc>
		<item type="weapon" />
	</player>
	<player id="p2">
		<item type="weapon" />
	</player>
	<player id="p3">
		<item type="weapon" />
	</player>
	<player id="p4">
		<item type="weapon" />
	</player>
	<player id="p5">
		<item type="weapon" />
	</player>	
</players>

And to parse and work with the returned DOM in Lure:

require("lure.lua")

playersXML = lure.load("players.xml")

player1 = playersXML.documentElement.firstChild
print(player1.id)
--OUTPUT: p1

print(player1.childNodes[1].childNodes[1].data)
--OUTPUT: I am player 1

player1.childNodes[2].getAttribute("type")
--OUTPUT: weapon