Getting Started with lua-e4x

How to get started with lua-e4x.

lua_e4x is a Lua implementation of E4X (ECMAScript for XML). It is modeled after the Adobe E4X implementation.

For more information, look at the page lua-e4x Documentation or check out the unit tests to see what the module can do.

I have listed an example below.

Installation

For installation instructions see !! file not found !!.

lua_e4x Overview

E4X is an idea which was introduced back in 2007 to describe a way to more easily work with XML data.

Though XML isn't as popular these days because JSON is used more often as a data-exchange format. However, XML isn't without its strengths and continues to be used today.

lua_e4x Basics

Usage Overview

Here is a simple example of working with XML using lua_e4x. First is the XML data file,

XML Example Data: 'spec/xml/test-01.xml'

<?xml version="1.0"?>
<order>
    <book ISBN="0942407296">
        <title>Baking Extravagant Pastries with Kumquats</title>
        <author>
            <lastName>Contino</lastName>
            <firstName>Chuck</firstName>
        </author>
        <pageCount>238</pageCount>
    </book>
    <book ISBN="0865436401" publisher="Prentice Hall">
        <title>Emu Care and Breeding</title>
        <editor>
            <lastName>Case</lastName>
            <firstName>Justin</firstName>
        </editor>
        <pageCount>115</pageCount>
    </book>
</order>

Lua E4X Basic Example

--====================================================================--
--== Imports

local E4X = require 'lua_e4x'
local File = require 'lua_files' -- soon not needed


--====================================================================--
--== Setup, Constants

local data, xml


--====================================================================--
--== Main
--====================================================================--

data = File.readFileContents( './spec/xml/test-01.xml' )
xml = E4X.parse( data )

-- this will find books with editor field and get last name
print( xml.book.editor.lastName:toString() )

-- finds books with titles and loops through results
for i, title in xml.book.title:nodes() do
    print( title:toString() )
end

Example Output

Case
Baking Extravagant Pastries with Kumquats
Emu Care and Breeding