Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tag with both attr and value #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example4-table2xml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ print("xml2lua v" .. xml2lua._VERSION.."\n")

local people = {
person = {
{_attr={type="natural"}, name="Manoel", city="Palmas-TO"},
{_attr={type="natural"}, name="Breno", city="Palmas-TO"},
{_attr={type="natural"}, name= {_val = "Manoel", _attr = {tt = "bb"}}, city="Palmas-TO"},
{_attr={type="natural"}, name= {_val = "Breno"}, city="Palmas-TO"},
{_attr={type="legal"}, name="University of Brasília", city="Brasília-DF"}
}
}
Expand Down
14 changes: 14 additions & 0 deletions xml2lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ function xml2lua.isTableEmpty(obj)
return true
end

function xml2lua.isSingleValue(obj)
local have_value = false
for k, _ in pairs(obj) do
if (k == '_val') then
have_value = true
elseif (k ~= '_attr') then
return false
end
end
return have_value
end

function xml2lua.parseTableToXml(obj, tagName, level)
if (tagName ~= '_attr') then
if (type(obj) == 'table') then
Expand All @@ -244,6 +256,8 @@ function xml2lua.parseTableToXml(obj, tagName, level)
end
elseif xml2lua.isTableEmpty(obj) then
xml2lua.addTagValueAttr(tagName, "", obj._attr, level)
elseif xml2lua.isSingleValue(obj) then
xml2lua.addTagValueAttr(tagName, obj._val, obj._attr, level)
else
xml2lua.startTag(tagName, obj._attr, level)
for tag, value in pairs(obj) do
Expand Down