XML/Output: Difference between revisions
Content deleted Content added
→{{header|PureBasic}}: Corrected code sample to use escaped characters |
Added Lua version |
||
Line 1,254: | Line 1,254: | ||
</CharacterRemarks> |
</CharacterRemarks> |
||
</lang> |
</lang> |
||
=={{header|Lua}}== |
|||
Using the LuaXML library as available via luarocks. Note that strings in Lua can be enclosed in either single or double quotes to help reduce the need for escape characters. |
|||
<lang Lua>require("LuaXML") |
|||
function addNode(parent, nodeName, key, value, content) |
|||
local node = xml.new(nodeName) |
|||
table.insert(node, content) |
|||
parent:append(node)[key] = value |
|||
end |
|||
root = xml.new("CharacterRemarks") |
|||
addNode(root, "Character", "name", "April", "Bubbly: I'm > Tam and <= Emily") |
|||
addNode(root, "Character", "name", "Tam O'Shanter", 'Burns: "When chapman billies leave the street ..."') |
|||
addNode(root, "Character", "name", "Emily", "Short & shrift") |
|||
print(root)</lang> |
|||
{{out}} |
|||
<pre><CharacterRemarks> |
|||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
|||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
|||
<Character name="Emily">Short & shrift</Character> |
|||
</CharacterRemarks></pre> |
|||
Note also that LuaXML escapes quote marks and apostrophes, which makes the output slightly different to the task requirement. This can be 'fixed' if necessary using Lua's in-built string.gsub function: |
|||
<lang Lua>xmlStr = xml.str(root):gsub("'", "'"):gsub(""", '"') |
|||
print(xmlStr)</lang> |
|||
{{out}} |
|||
<pre><CharacterRemarks> |
|||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
|||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street |
|||
..."</Character> |
|||
<Character name="Emily">Short & shrift</Character> |
|||
</CharacterRemarks></pre> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |