lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> When I try to print out tbl[1][1]:

> io.write(tostring(tbl[1][cusomer_cnt]))
>
> the print out result is:  nil

 

Apart from the typo (cusomer_cnt instead of customer_cnt), I’m not sure what you expect from the call.

This is what is contained in tbl:

 

{

  [1] = "

",

  [2] =

  {

    [1] = "

",

    [2] =

    {

      [1] = "

",

      [2] =

      {

        [1] = "XYZ Market",

        ["attr"] =

        {

        },

        ["tag"] = "CompanyName",

      },

      [3] = "

",

      [4] =

      {

        [1] = "John Doe",

        ["attr"] =

        {

        },

        ["tag"] = "ContactName",

      },

      [5] = "

",

      [6] =

      {

        [1] = "Marketing Manager",

        ["attr"] =

        {

        },

        ["tag"] = "ContactTitle",

      },

      [7] = "

",

      [8] =

      {

        [1] = "(212) 555-1212",

        ["attr"] =

        {

        },

        ["tag"] = "Phone",

      },

      [9] = "

",

      [10] =

      {

        [1] = "

",

        [2] =

        {

          [1] = "100 Park Avenue.",

          ["attr"] =

          {

          },

          ["tag"] = "Address",

        },

        [3] = "

",

        [4] =

        {

          [1] = "New York",

          ["attr"] =

          {

          },

          ["tag"] = "City",

        },

        [5] = "

",

        [6] =

        {

          [1] = "NY",

          ["attr"] =

          {

          },

          ["tag"] = "State",

        },

        [7] = "

",

        ["attr"] =

        {

        },

        ["tag"] = "FullAddress",

      },

      [11] = "

",

      ["attr"] =

      {

        [1] = "CustomerID",

        ["CustomerID"] = "XYZ",

      },

      ["tag"] = "Customer",

    },

    [3] = "

",

    ["attr"] =

    {

    },

    ["tag"] = "Customers",

  },

  [3] = "

",

  ["attr"] =

  {

  },

  ["tag"] = "Root",

}

 

Check the LuaExpat documentation [1] for details on the table format.

 

Tom

 

[1] http://matthewwild.co.uk/projects/luaexpat/lom.html

 

From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Jim zhang
Sent: Wednesday, February 06, 2013 3:56 PM
To: Lua mailing list
Subject: xml parsing problem

 

I try to use lxp.lom to parse xml file, but it gives empty table. Here is the code:

-- setup XML parser
local lom = require "lxp.lom"

--define global variables
tbl = {}
customer_cnt = 1

function initialize()
-- create a file handle to the file
local handle = io.open("testxml.xml","r")
  
-- read and parse the XML document
tbl = lom.parse(handle:read("*a"))
handle:close()
end;

When I try to print out tbl[1][1]:
 
io.write(tostring(tbl[1][cusomer_cnt]))

the print out result is:  nil

 

testxml.xml file:

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Customers>
<Customer CustomerID="XYZ">
<CompanyName>XYZ Market</CompanyName>
<ContactName>John Doe</ContactName>
<ContactTitle>Marketing Manager</ContactTitle>
<Phone>(212) 555-1212</Phone>
<FullAddress>
<Address>100 Park Avenue.</Address>
<City>New York</City>
<State>NY</State>
</FullAddress>
</Customer>
</Customers>
</Root>

 

Not sure what I did incorrectly here, I would like to get some help. Thank you very much!

 

Jim