XML

From Pragma
Jump to: navigation, search

Name: wv_xml

State:

SHARED

Description

By loading this module, the following classes are made available:

As well as the xml library.

Examples

This example will parse a simple XML-string and iterate through the data.

local r = engine.load_library("wv_xml")
if(r ~= true) then
	console.print("WARNING: An error occured trying to load the 'wv_xml' module: ",r)
	return
end

local xmlStr = [[
<item flags="25">
	<name>Apple</name>
	<desc>Fruity</desc>
	<value>1.75</value>
</item>
<item flags="25">
	<name>Pear</name>
	<desc>Fruity</desc>
	<value>2.25</value>
</item>
]]

local bSuccess,ret,errLine,errChar = xml.parse(xmlStr)
if(bSuccess == false) then
	console.print_messageln("A parse error has occurred at line " .. errLine .. " at character " .. errChar .. ": " .. ret)
else
	local xml = ret
	console.print(xml:ToString())

	local items = {}
	local item = xml:GetFirstNode("item")
	while(item ~= nil) do
		table.insert(items,{
			name = item:GetFirstNode("name"):GetValue(),
			desc = item:GetFirstNode("desc"):GetValue(),
			val = item:GetFirstNode("value"):GetValue(),
			flags = item:GetFirstAttribute("flags"):GetValue()
		})
		item = item:GetNextSibling("item")
	end
	console.print_table(items)
end

See Also