Skip to content

Commit 2f4f17e

Browse files
committed
Document xml.decode metamethod
1 parent 50b7f30 commit 2f4f17e

File tree

1 file changed

+17
-2
lines changed
  • docs/Runtime Environment/Library Reference

1 file changed

+17
-2
lines changed

docs/Runtime Environment/Library Reference/XML.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ Returns a table representing the XML data.
77
```pluto
88
local xml = require "pluto:xml"
99
10-
print(dumpvar(xml.decode([[
10+
local root = xml.decode([[
1111
<entries>
1212
<entry type="primary">
1313
<name>primary</name>
1414
</entry>
1515
</entries>
16-
]])))
16+
]])
1717
18+
print(dumpvar(root))
1819
--> {
1920
--> ["tag"] = string(7) "entries",
2021
--> ["children"] = {
@@ -35,6 +36,20 @@ print(dumpvar(xml.decode([[
3536
--> },
3637
--> }
3738
```
39+
40+
For ease of use, the returned tables have an `__index` metamethod:
41+
```pluto
42+
-- root is <entries>, so its first child is <entry>.
43+
-- we make use of the metamethod to get to <name>:
44+
print(dumpvar(root.children[1].name))
45+
--> {
46+
--> ["tag"] = string(4) "name",
47+
--> ["children"] = {
48+
--> [1] = string(7) "primary",
49+
--> },
50+
--> }
51+
```
52+
3853
If there is no single root element, a "body" element is implied as the root:
3954
```pluto
4055
local xml = require "pluto:xml"

0 commit comments

Comments
 (0)