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 | 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(); |