[library] libxml

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
rdlaitila
Prole
Posts: 8
Joined: Tue Nov 01, 2011 5:26 pm

[library] libxml

Post by rdlaitila »

Hello All,

I would like to present my newly released library libxml. The code was ripped from, and slightly re-factored for more strict XML use from my other ongoing project LURE. The library is the first incarnation to allow lovers to load, save, and traverse XML documents using standard XML DOM interfaces. Let me show you some examples:

XML Document:

Code: Select all

<players>
	<player name="Player1">
		<location xPos="100" yPos="200"/>
		<item type="weapon" class="rifle" />
		<item type="ammo" class="rifle" />
		<item type="healthpack" />
		<item type="healthpack" />
		<item type="healthpack" />
	</player>
	<player name="Player2">
		<location xPos="500" yPos="200"/>
		<item type="weapon" class="pistol" />
		<item type="ammo" class="pistol" />
		<item type="healthpack" />		
	</player>
</players>
Loading the players.xml document:

Code: Select all

function love.load()
	require "libxml//libxml"
	
	xmldoc = libxml.load("player.xml")	
end
You can now traverse the xml document much like you would expect from languages such as javascript etc:

Code: Select all

function love.load()
	require "libxml//libxml"
	
	xmldoc = libxml.load("player.xml")	
	print(xmldoc.documentElement.childNodes[1].getAttribute("name"))
        --OUTPUT: Player1
end
You can even create new elements and insert them into the document:

Code: Select all

function love.load()
	require "libxml//libxml"
	
	xmldoc = libxml.load("player.xml")	
	newPlayer = xmldoc.createElement("player")
	newPlayer.setAttribute("name", "player3")
	
	newItem = xmldoc.createElement("item")
	newItem.setAttribute("type", "healthpack")
	
	newPlayer.appendChild(newItem)	
	xmldoc.documentElement.appendChild(newPlayer)	
end
And to save your newly modified document to file, simply call the libxml.save() function:

Code: Select all

function love.load()
	require "libxml//libxml"
	
	xmldoc = libxml.load("player.xml")
	
	newPlayer = xmldoc.createElement("player")
	newPlayer.setAttribute("name", "player3")
	
	newItem = xmldoc.createElement("item")
	newItem.setAttribute("type", "healthpack")
	newItem.isSelfClosing = true
	
	newPlayer.appendChild(newItem)	
	xmldoc.documentElement.appendChild(newPlayer)
	
	libxml.save(xmldoc, "player-new.xml")
end
The above would generate the following new xml in player-new.xml in your user directory:

Code: Select all

<players>
	<player name="Player1">
		<location xPos="100" yPos="200" />
		<item type="weapon" class="rifle" />
		<item type="ammo" class="rifle" />
		<item type="healthpack" />
		<item type="healthpack" />
		<item type="healthpack" />
	</player>
	<player name="Player2">
		<location xPos="500" yPos="200" />
		<item type="weapon" class="pistol" />
		<item type="ammo" class="pistol" />
		<item type="healthpack" />
	</player>
	<player name="player3">
		<item type="healthpack" />
	</player>
</players>
Lastly for now, the library also does dynamic node name traversal (XPath equivalent not implemented):

Code: Select all

function love.load()
	require "libxml//libxml"
	
	xmldoc = libxml.load("player.xml")	
	print( xmldoc.players.player[2].getAttribute("name") )
	--OUTPUT: Player 2
	print( xmldoc.players.player[2].location.getAttribute("xPos") )
	--OUTPUT: 500
	print( xmldoc.players.player[2].item[2].getAttribute("type") )
	--OUTPUT: healthpack
end
KNOWN LIMITATIONS
  • - Does not understand XML Namespaces
    - Does not parse document <? xml ?> tags yet
    - Does not parse document processing instructions
    - Does not have XPath capabilities yet
    - The parser is non-validating, so you must ensure your xml structure is proper
    - Everything including node tagNames are case-sensative
    - Does not expand entity references
Let me know what you guys think. While its an ealy release, if you guys find any show-stopper bugs or would like a feature implimented please visit the gitup issues page to submit bugs/feature requests. https://github.com/admin36/libxml/issues
Llamageddon
Prole
Posts: 3
Joined: Sun Aug 03, 2014 9:42 pm

Re: [library] libxml

Post by Llamageddon »

This looks really awesome, but without a license, it's unusable, you really should add one. If you're not sure, and want it to be open source, just go with MIT, find a copy, and put it in the github repo.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests