lxp
Index: Articles Classes Hooks Quick navigation: cArrowEntity cBeaconEntity cBlockArea cBlockEntity cBlockEntityWithItems cBlockInfo cBoundingBox cBrewingstandEntity cChatColor cChestEntity cChunkDesc cClientHandle cCommandBlockEntity cCompositeChat cCraftingGrid cCraftingRecipe cCryptoHash cCuboid cDispenserEntity cDropperEntity cDropSpenserEntity cEnchantments cEntity cEntityEffect cExpBottleEntity cFile cFireChargeEntity cFireworkEntity cFloater cFlowerPotEntity cFurnaceEntity cGhastFireballEntity cHangingEntity cHopperEntity cIniFile cInventory cItem cItemFrame cItemGrid cItems cJson cJukeboxEntity 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 cStatManager cStringCompression cTCPLink cTeam cThrownEggEntity cThrownEnderPearlEntity cThrownSnowballEntity cTNTEntity cTracer cUDPEndpoint cUrlClient cUrlParser cWebAdmin cWindow cWitherSkullEntity cWorld HTTPFormData HTTPRequest HTTPTemplateRequest ItemCategory lxp sqlite3 TakeDamageInfo tolua Vector3d Vector3f Vector3i Globals | Contentslxp classThis 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
Functions
Parser callbacksThe 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.
XMLParser objectThe 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.
Code exampleThe 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(); |