1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
<!DOCTYPE html>
<html>
<head>
<title>LuaExpat: XML Expat parsing for the Lua programming language</title>
<link rel="stylesheet" href="./doc.css" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"><a href="https://github.com/lunarmodules/luaexpat">
<img alt="LuaExpat logo" src="luaexpat.png"/>
</a></div>
<div id="product_name"><big><strong>LuaExpat</strong></big></div>
<div id="product_description">XML Expat parsing for the Lua programming language</div>
</div> <!-- id="product" -->
<div id="main">
<div id="navigation">
<h1>LuaExpat</h1>
<ul>
<li><a href="index.html">Home</a>
<ul>
<li><a href="index.html#overview">Overview</a></li>
<li><a href="index.html#status">Status</a></li>
<li><a href="index.html#download">Download</a></li>
<li><a href="index.html#history">History</a></li>
<li><a href="index.html#references">References</a></li>
<li><a href="index.html#credits">Credits</a></li>
</ul>
</li>
<li><a href="manual.html">Manual</a>
<ul>
<li><a href="manual.html#introduction">Introduction</a></li>
<li><a href="manual.html#building">Building</a></li>
<li><a href="manual.html#installation">Installation</a></li>
<li><a href="manual.html#parser">Parser Objects</a></li>
</ul>
</li>
<li><a href="examples.html">Examples</a></li>
<li><strong>Additional parsers</strong>
<ul>
<li><strong>Lua Object Model</strong></li>
<li><a href="totable.html">Parse to table</a></li>
<li><a href="threat.html">Threat protection</a></li>
</ul>
</li>
<li><a href="https://github.com/lunarmodules/luaexpat">Project</a>
<ul>
<li><a href="https://github.com/lunarmodules/luaexpat/issues">Bug Tracker</a></li>
<li><a href="https://github.com/lunarmodules/luaexpat">Source code</a></li>
<li><a href="https://lunarmodules.github.io/luaexpat/">Documentation</a></li>
</ul>
</li>
<li><a href="license.html">License</a></li>
</ul>
</div> <!-- id="navigation" -->
<div id="content">
<h2><a name="introduction"></a>Introduction</h2>
<p>Lua Object Model (LOM) is a representation of XML elements
through Lua data types. Currently it is not supposed to be 100%
complete, but simple. LuaExpat provides an implementation of LOM that
gets an XML document and transforms it to a Lua table.
</p>
<h2><a name="characteristics"></a>Characteristics</h2>
<p>The model represents each XML element as a Lua table. A LOM
table has three special characteristics:</p>
<ul>
<li>a special field called <strong><code>tag</code></strong> that holds the
element's name;</li>
<li>an optional field called <strong><code>attr</code></strong> that stores
the element's attributes; and</li>
<li>the element's children are stored at the <em>array-part</em> of
the table. A child could be an ordinary string or another XML
element that will be represented by a Lua table following these
same rules.</li>
</ul>
<p>The special field <strong><code>attr</code></strong> is a Lua table that
stores the XML element's attributes as pairs
<em><key>=<value></em>. To assure an order (if
necessary), the sequence of <em>key</em>s could be placed at the
<em>array-part</em> of this same table.</p>
<h3>Functions</h3>
<dl class="reference">
<dt><strong>lom.parse(string|function|table|file[, opts])</strong></dt>
<dd>Parses the input into the LOM table format and returns it. The input can be;
<ul>
<li><em>string</em>: the entire XML document as a string</li>
<li><em>function</em>: an iterator that returns the next chunk of the
XML document on each call, and returns <em>nil</em> when finished</li>
<li><em>table</em>: an array like table that contains the chunks
that combined make up the XML document</li>
<li><em>file</em>: an open file handle from which the XML document will
be read line-by-line, using <code>read()</code>. <strong>Note</strong>:
the file will not be closed when done.</li>
</ul>
The second parameter <em>opts</em> is an options table that supports the
following options;
<ul>
<li><em>separator (string)</em>: the namespace separator character to use, setting
this will enable namespace aware parsing.</li>
<li><em>threat (table)</em>: a <a href="threat.html#options">threat
protection options</a> table. If provided the threat protection parser
will be used instead of the regular <em>lxp</em> parser.</li>
</ul>
Upon parsing errors it will return <code>nil, err, line, col, pos</code>.
</dd>
<dt><strong>lom.find_elem(node, tag)</strong></dt>
<dd>Traverses the tree recursively, and returns the first element that matches
the <em>tag</em>. Parameter <em>tag</em> (string) is the tag name to look for.
The <em>node</em> table can be the result from the <em>parse</em> function, or
any of its children.</dd>
<dt><strong>lom.list_children(node[, tag])</strong></dt>
<dd>Iterator returning all child tags of a node (non-recursive). It will only
children that are tags, and will skip text-nodes.
The <em>node</em> table can be the result from the <em>parse</em> function, or
any of its children.
If the optional parameter <em>tag</em> (string) is given, then the iterator
will only return tags that match the tag name.
</dd>
</dl>
<h2><a name="examples"></a>Examples</h2>
<p>For a simple string like</p>
<pre class="example">
s = [[<abc a1="A1" a2="A2">inside tag `abc'</abc>]]
</pre>
<p>A call like</p>
<pre class="example">
tab = lxp.lom.parse (s))
</pre>
<p>Would result in a table equivalent to</p>
<pre class="example">
tab = {
["attr"] = {
[1] = "a1",
[2] = "a2",
["a2"] = "A2",
["a1"] = "A1",
},
[1] = "inside tag `abc'",
["tag"] = "abc",
}
</pre>
<p>Now an example with an element nested inside another element</p>
<pre class="example">
tab = lxp.lom.parse(
[[<qwerty q1="q1" q2="q2">
<asdf>some text</asdf>
</qwerty>]]
)
</pre>
<p>The result would have been a table equivalent to</p>
<pre class="example">
tab = {
[1] = "\
",
[2] = {
["attr"] = {
},
[1] = "some text",
["tag"] = "asdf",
},
["attr"] = {
[1] = "q1",
[2] = "q2",
["q2"] = "q2",
["q1"] = "q1",
},
[3] = "\
",
["tag"] = "qwerty",
}
</pre>
<p>Note that even the <em>new-line</em> and <em>tab</em> characters are stored
on the table.</p>
</div> <!-- id="content" -->
</div> <!-- id="main" -->
</div> <!-- id="container" -->
</body>
</html>
|