Question

I am using luaxml to convert a Lua table to xml. I have a problem using luaxml, for example, I have a lua table that like this

t = {name="John", age=23, contact={email="john@gmail.com", mobile=12345678}}

when I tried to use LuaXML,

local x = xml.new("person")
x:append("name")[1] = John
x:append("age")[1] = 23
x1 = xml.new("contact")  
x1:append("email")[1] = "john@gmail.com"
x1:append("mobile")[1] = 12345678
x:append("contact")[1] =  x1

The resulting xml becomes:

<person> 
  <name>John</name>
  <age>23</age>
  <contact>
    <contact>
      <email>john@gmail.com</email>
      <mobile>12345678</mobile>
    </contact>
  </contact>
</person>`

There are 2 contacts in the xml. What should I do to make the Xml correct?

In addition, how do I convert XML back to Lua table?

Was it helpful?

Solution

your syntax is just a bit off, you're creating a new table for the contacts, and then appending a 'contact' node and assigning an additional one at the same time with this code:

x1 = xml.new("contact")  
x1:append("email")[1] = "john@gmail.com"
x1:append("mobile")[1] = 12345678
x:append("contact")[1] =  x1

which should really just be something like:

local contact = xml.new("contact")
contact.email = xml.new("email")
table.insert(contact.email, "john@gmail.com")

contact.mobile = xml.new("mobile")
table.insert(contact.mobile, 12345678)

remember that each 'node' is its own table value, which is what xml.new() returns.

the below code creates the xml correctly when you call xml.save(x, "\some\filepath"). the thing to remember is that whenever you call xml.new(), you get a table back, i think the decision there was that it makes it easy to set attributes... but makes the syntax for adding simple values a little more verbose.

-- generate the root node
local root = xml.new("person")

-- create a new name node to append to the root
local name = xml.new("name")
-- stick the value into the name tag
table.insert(name, "John")

-- create the new age node to append to the root
local age = xml.new("age")
-- stick the value into the age tag
table.insert(age, 23)

-- this actually adds the 'name' and 'age' tags to the root element
root:append(name)
root:append(age)

-- create a new contact node
local contact = xml.new("contact")
-- create a sub tag for the contact named email
contact.email = xml.new("email")
-- insert its value into the email table
table.insert(contact.email, "john@gmail.com")

-- create a sub tag for the contact named mobile
contact.mobile = xml.new("mobile")
table.insert(contact.mobile, 12345678)

-- add the contact node, since it contains all the info for its
-- sub tags, we don't have to worry about adding those explicitly.
root.append(contact)

it should become fairly clear following that example how you could drill down arbitrarily deep. you could even write functions to easily facilitate creating child tags to make the code much less verbose...

OTHER TIPS

local x = xml.new("person")
x:append("name")[1] = John
x:append("age")[1] = 23
x1 = x:append("contact")
x1:append("email")[1] = "john@gmail.com"
x1:append("mobile")[1] = 12345678

print(x)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top