lxp


Index:
Articles
Classes
Hooks

Quick navigation:
BannerPattern
BossBarColor
BossBarDivisionType
cArrowEntity
cBeaconEntity
cBedEntity
cBlockArea
cBlockEntity
cBlockEntityWithItems
cBlockInfo
cBoat
cBoundingBox
cBrewingstandEntity
cChatColor
cChestEntity
cChunkDesc
cClientHandle
cColor
cCommandBlockEntity
cCompositeChat
cCraftingGrid
cCraftingRecipe
cCryptoHash
cCuboid
cDispenserEntity
cDropperEntity
cDropSpenserEntity
cEnchantments
cEnderCrystal
cEntity
cEntityEffect
cExpBottleEntity
cExpOrb
cFallingBlock
cFile
cFireChargeEntity
cFireworkEntity
cFloater
cFlowerPotEntity
cFurnaceEntity
cGhastFireballEntity
cHangingEntity
cHopperEntity
cIniFile
cInventory
cItem
cItemFrame
cItemGrid
cItems
cJson
cJukeboxEntity
cLeashKnot
cLineBlockTracer
cLuaWindow
cMap
cMapManager
cMobHeadEntity
cMobSpawnerEntity
cMojangAPI
cMonster
cNetwork
cNoteEntity
cObjective
cPainting
cPawn
cPickup
cPlayer
cPlugin
cPluginLua
cPluginManager
cProjectileEntity
cRankManager
cRoot
cScoreboard
cServer
cServerHandle
cSignEntity
cSplashPotionEntity
cStringCompression
cTCPLink
cTeam
cThrownEggEntity
cThrownEnderPearlEntity
cThrownSnowballEntity
cTNTEntity
cUDPEndpoint
cUrlClient
cUrlParser
CustomStatistic
cUUID
cWebAdmin
cWindow
cWitherSkullEntity
cWorld
EffectID
HTTPFormData
HTTPRequest
HTTPTemplateRequest
ItemCategory
lxp
SmokeDirection
sqlite3
StatisticsManager
TakeDamageInfo
tolua
Vector3d
Vector3f
Vector3i
Globals

Contents


lxp class

This class provides an interface to the XML parser, LuaExpat. It provides a SAX interface with an incremental XML parser.

With an event-based API like SAX the XML document can be fed to the parser in chunks, and the parsing begins as soon as the parser receives the first document chunk. LuaExpat reports parsing events (such as the start and end of elements) directly to the application through callbacks. The parsing of huge documents can benefit from this piecemeal operation.

See the online LuaExpat documentation for details on how to work with this parser. The code examples below should provide some basic help, too.


Constants

NameValueNotes
Copyright (C) 2003-2012 Kepler Project
_DESCRIPTION LuaExpat is a SAX XML parser based on the Expat library
_VERSION LuaExpat 1.3.0

Functions

NameParametersReturn valueNotes
new
CallbacksTabletable
SeparatorCharstring
XMLParser objecttable
Creates a new XML parser object, with the specified callbacks table and optional separator character.

Parser callbacks

The callbacks table passed to the new() function specifies the Lua functions that the parser calls upon various events. The following table lists the most common functions used, for a complete list see the online LuaExpat documentation.

Function nameParametersNotes
CharacterDataParser, stringCalled when the parser recognizes a raw string inside the element
EndElementParser, ElementNameCalled when the parser detects the ending of an XML element
StartElementParser, ElementName, AttributesTableCalled when the parser detects the start of an XML element. The AttributesTable is a Lua table containing all the element's attributes, both in the array section (in the order received) and in the dictionary section.

XMLParser object

The XMLParser object returned by lxp.new provides the functions needed to parse the XML. The following list provides the most commonly used ones, for a complete list see the online LuaExpat documentation.
  • close() - closes the parser, freeing all memory used by it.
  • getCallbacks() - returns the callbacks table for this parser.
  • parse(string) - parses more document data. the string contains the next part (or possibly all) of the document. Returns non-nil for success or nil, msg, line, col, pos for error.
  • stop() - aborts parsing (can be called from within the parser callbacks).

Code example

The following code reads an entire XML file and outputs its logical structure into the console:
local Depth = 0;

-- Define the callbacks:
local Callbacks = {
	CharacterData = function(a_Parser, a_String)
		LOG(string.rep(" ", Depth) .. "* " .. a_String);
	end

	EndElement = function(a_Parser, a_ElementName)
		Depth = Depth - 1;
		LOG(string.rep(" ", Depth) .. "- " .. a_ElementName);
	end

	StartElement = function(a_Parser, a_ElementName, a_Attribs)
		LOG(string.rep(" ", Depth) .. "+ " .. a_ElementName);
		Depth = Depth + 1;
	end
}

-- Create the parser:
local Parser = lxp.new(Callbacks);

-- Parse the XML file:
local f = io.open("file.xml", "rb");
while (true) do
	local block = f:read(128 * 1024);  -- Use a 128KiB buffer for reading
	if (block == nil) then
		-- End of file
		break;
	end
	Parser:parse(block);
end

-- Signalize to the parser that no more data is coming
Parser:parse();

-- Close the parser:
Parser:close();
Generated by APIDump on 2024-12-18 00:46:06, Build ID 101, Commit b92dc1cda3576ffc4c85347b0e8449d5adb87ef6