Libxml Module

BlitzMax Forums/BlitzMax Programming/Libxml Module

Sean,

Here is an example i cooked up. give it a whirl:

This is the BlitzMax code:
SuperStrict


Import BaH.Libxml


Graphics 800,600
SetBlend(ALPHABLEND)

Local xmldoc:TxmlDoc = TxmlDoc.parseFile("test.xml")

Print xmldoc.getURL()

Local game:TxmlNode = xmldoc.getRootElement()

Print game.getName()


Local children:TList = game.getChildren()
Global EntityList:TList = CreateList()

Print children.count()


	For Local node:TxmlNode = EachIn children
		Local name:String , xpos:Int , ypos:Int
		Local red:Int , green:Int , blue:Int 	
		Local angle:Float , scale:Float
		
		If node.getName() = "entity" Then
		
			  Local entitynodes:TList = node.getChildren()
			
			   name:String = node.getAttribute("name")
			   xpos:Int = Int(node.getAttribute("xpos"))
			   ypos:Int = Int(node.getAttribute("ypos"))
			
			For Local en:TxmlNode = EachIn entitynodes
				If en.getname() = "color" Then
					 red:Int = Int(en.getAttribute("red"))
					 green:Int = Int(en.getAttribute("green"))
					 blue:Int = Int(en.getAttribute("blue"))
				EndIf
				If en.getname() = "angle" Then
					angle:Float = Float(en.getcontent())
				EndIf
				If en.getname() = "scale" Then
					scale:Float = Float(en.getcontent())
				EndIf

			Next
			If scale = 0 Then scale = 1
			Local foo:Tentity = Tentity.create(name,xpos,ypos,angle,scale,red,green,blue)
			
		EndIf
	Next



For Local node:TxmlNode = EachIn children
	Print node.getName()
Next

'doc.setCompressMode(0)
'doc.saveFile("aaa.xml")


Type Tentity
	Field name:String
	Field xpos:Int
	Field ypos:Int
	Field red:Int
	Field green:Int
	Field blue:Int
	Field angle:Float
	Field scale:Float
	
	Method draw()
		SetAlpha 0.65
		SetRotation angle
		SetScale (scale,scale)
		Local w:Int = TextWidth(name)*1.1
		Local h:Int = TextHeight(name)*1.1
		SetColor red,green,blue
		DrawRect xpos,ypos,w,h
		SetColor Abs(red-128),Abs(green-128),Abs(blue-128)
		SetAlpha 1
		DrawText name,xpos,ypos
	End Method
	
	Function create:Tentity(n:String,xp:Int,yp:Int,an:Float,sc:Float=1.0,r:Int=255,g:Int=255,b:Int=255)
		Local temp:Tentity = New Tentity
		temp.name = n
		temp.scale = sc
		temp.xpos = xp
		temp.ypos = yp
		temp.angle = an
		temp.red = r
		temp.green = g
		temp.blue = b
	
		ListAddLast(EntityList,temp)
		Return temp
	End Function
End Type

Repeat

For Local e:Tentity = EachIn Entitylist
	e.draw()

Next
Flip
Cls
Until KeyDown(KEY_ESCAPE)





Save this as "test.xml" in the same directory as above code snippet
This is the XML data:


<?xml version="1.0"?>
<Game>
    <!--This is a Comment-->
    <!--Comments are ignored-->
    <entity name="Eric" xpos="100" ypos="100">
        <color blue="0" green="255" red="100"></color>
        <angle>23</angle>
    </entity>
    <entity name="Bill" xpos="200" ypos="200">
        <color blue="0" green="255" red="100"></color>
        <angle>43</angle>
    </entity>
    <entity name="John Jacob Jingle Heimer Schmidt" xpos="300" ypos="200">
        <color blue="255" green="255" red="100"></color>
        <angle>-13</angle>
    </entity>
    <entity name="Carlos" xpos="20" ypos="20">
        <color blue="25" green="5" red="5"></color>
        <angle>30</angle>
    </entity>
    <entity name="BlitzMax rocks!" xpos="200" ypos="200">
        <color blue="240" green="0" red="0"></color>
        <angle>-15</angle>
    </entity>
    <entity name="Madison" xpos="10" ypos="400">
        <color blue="200" green="0" red="0"></color>
        <angle>-31</angle>
    </entity>
    <entity name="Helena" xpos="180" ypos="290">
        <color blue="0" green="55" red="0"></color>
        <angle>15</angle>
    </entity>
    <entity name="Error! Error Abort! A#$BorT.a" xpos="500" ypos="400">
        <color blue="60" green="0" red="0"></color>
        <angle>25</angle>
    </entity>
    <entity name="LibXML is cool (thanks Brucey!)" xpos="260" ypos="200">
        <color blue="5" green="10" red="10"></color>
        <angle>12</angle>
        <scale>2.0</scale>
    </entity>
</Game>