manoelcampos / xml2lua

XML Parser written entirely in Lua that works for Lua 5.1+. Convert XML to and from Lua Tables 🌖💱
MIT License
290 stars 74 forks source link

Accessing children with index rather than name #18

Closed slavidodo closed 6 years ago

slavidodo commented 6 years ago

Ok, in pugixml you can easily do something like: for (pugi::xml_node& node : root.children()) { ... }

This is useful if you want the nodes in order. My case is that i am using xml to handle some ui elements, so the order of the elements must be known in order to view them correctly.

<contents>
   <label>Some text</label>
   <image source="logo.png" />
   <textarea placeholder="..." />
   <label>Enjoying?</label>
</contents>

Current behaviour

root.contents.label -> table (n=2)
root.contents.image -> table (n=1)
root.contents.textarea -> table(n=1)

Expected behavior

-- same as above
-- also
root.children -> ordered children

This doesn't use more memory since tables in lua are actually passed by ref. So children table will just have a reference to the original tables sorted by element name.

slavidodo commented 6 years ago

This is done already in the dom element.

manoelcampos commented 6 years ago

Maybe you got the order defined in the XML file just by change. Lua pairs function defines an arbitrary order to iterate over the table elements when they don't have a numbered index.