getElements() accepts no parameters and returns all elements inside this node. You can take the "map" part out, I believe. Since 'map' is your root node, you don't need to reference it at all; it will be understood that it is being used.local l_xml_map = l_xml:getElements("map")
I've fixed it. In Lua modules, they do not have global scope by default, so you need to allow them to access global functions and variables. That's why you see all the "local <function> = <function>" at the top of module files. Thanks for pointing this out.attempt to call global 'error' (a nil value)
It is a lua native function, so I couldn't work it out, perhaps u know why? :/
I've also updated the attribute access in the module. I've added getAttribute() (basically the same as getElement(), only for attributes) to be used instead of directly accessing an attribute via the [] or . operators. There's also getAttributes() to return all attributes, and getAttributeCount() to return the number of attributes.
These changes should result in a very small speed increase (depending on how many attributes were being used before), and being able to use _NAME, _VALUE, and _ATTRIBUTES as attributes inside your XML document without screwing it up. And, finally, it should support better forward compatibility. Since you have not used attributes in your code, I don't think you'll need to make any changes.
Here's the new example:
Code: Select all
<root name="Root" type="Example">
<elem1>
<subelement value="1" />
<subelement value="2" />
<subelement value="3" />
</elem1>
<elem2 number="1234" string="This is a string" />
<elem3>Element 3 has a text value</elem3>
</root>
Code: Select all
function main()
local root = xml.open(getExecutionPath() .. "/test.xml");
printf("root._NAME: %s\n", root:getName()); -- "_NAME" should be 'root'
printf("root.name: %s\n", root:getAttribute("name")); -- "name" will be set in XML
printf("root.attribute count: %d\n", root:getAttributeCount());
local elem1 = root:getElement("elem1");
elements = elem1:getElements();
printf("\n---ELEM1---\n");
for i,v in pairs(elements) do
local name = v:getName();
local value = v:getAttribute("value");
printf(" ==> [%s]: %s\n", name, value);
end
local elem2 = root:getElement("elem2");
printf("\n---ELEM2---\n");
printf("elem2.number: %d\n", elem2:getAttribute("number"));
printf("elem2.string: %s\n", elem2:getAttribute("string"));
local elem3 = root:getElement("elem3");
printf("\n---ELEM3---\n");
printf("elem3._VALUE: %s\n", elem3:getValue());
end
startMacro(main);
root._NAME: root
root.name: Root
root.attribute count: 2
---ELEM1---
==> [subelement]: 1
==> [subelement]: 2
==> [subelement]: 3
---ELEM2---
elem2.number: 1234
elem2.string: This is a string
---ELEM3---
elem3._VALUE: Element 3 has a text value