[library] libxml
Posted: Thu Sep 06, 2012 11:25 am
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:
Loading the players.xml document:
You can now traverse the xml document much like you would expect from languages such as javascript etc:
You can even create new elements and insert them into the document:
And to save your newly modified document to file, simply call the libxml.save() function:
The above would generate the following new xml in player-new.xml in your user directory:
Lastly for now, the library also does dynamic node name traversal (XPath equivalent not implemented):
KNOWN LIMITATIONS
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>
Code: Select all
function love.load()
require "libxml//libxml"
xmldoc = libxml.load("player.xml")
end
Code: Select all
function love.load()
require "libxml//libxml"
xmldoc = libxml.load("player.xml")
print(xmldoc.documentElement.childNodes[1].getAttribute("name"))
--OUTPUT: Player1
end
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
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
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>
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
- - 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