Difference between revisions of "Function xml.parse()"

From wiki.netio-products.com
Jump to navigation Jump to search
Line 11: Line 11:
 
Consecutive spaces are trimmed to single space.
 
Consecutive spaces are trimmed to single space.
  
=== XmlElement Members ===
+
== XmlElement ==
* <i>string</i> <code>name</code> name of the node
+
 
 +
=== <i>string</i> <code>name</code> ===
 +
name of the node
 
  log(xml.parse("<note>something</note>").name) -- note
 
  log(xml.parse("<note>something</note>").name) -- note
* <i>string</i> <code>text</code> text content of the node (excluding the contents of child nodes). To read all content as a text, use .innerXml (see below)
+
 
 +
=== <i>string</i> <code>text</code> ===
 +
text content of the node (excluding the contents of child nodes). To read all content as a text, use .innerXml (see below)
 
  local data = xml.parse("<note>something &lt;big>really&lt;/big> good</note>")
 
  local data = xml.parse("<note>something &lt;big>really&lt;/big> good</note>")
 
  log(data.text) -- something good
 
  log(data.text) -- something good
* <i>string/nil</i> <code>attr(<i>string</i> name)</code> returns the value of a single attribute; if the attribute doesn't exists, ruturns <i>nil</i>
+
 
 +
=== <i>string/nil</i> <code>attr(<i>string</i> name)</code> ===
 +
returns the value of a single attribute; if the attribute doesn't exists, ruturns <i>nil</i>
 
  local data = xml.parse('<note from="ann" to="bob">hello</note>')
 
  local data = xml.parse('<note from="ann" to="bob">hello</note>')
 
  log(data.attr("from")) -- ann
 
  log(data.attr("from")) -- ann
* <i>table</i> <code>attr()</code> returns all attributes as a table
+
 
 +
=== <i>table</i> <code>attr()</code> ===
 +
returns all attributes as a table
 
  local data = xml.parse('<note from="ann" to="bob">hello</note>')
 
  local data = xml.parse('<note from="ann" to="bob">hello</note>')
 
  local attrs = data.attr()
 
  local attrs = data.attr()
 
  logf("%s -> %s", attrs.from, attrs["to"]) -- ann -> bob
 
  logf("%s -> %s", attrs.from, attrs["to"]) -- ann -> bob
* <i>XmlElement</i> <code>child(<i>string</i> name)</code> returns first child node with given name
+
 
 +
=== <i>XmlElement</i> <code>child(<i>string</i> name)</code> ===
 +
returns first child node with given name
 
  local data = [[
 
  local data = [[
 
   <papers>
 
   <papers>
Line 34: Line 44:
 
  local parsed = xml.parse(data)
 
  local parsed = xml.parse(data)
 
  log(parsed.child("book").text) -- Bible
 
  log(parsed.child("book").text) -- Bible
* <i>array</i> <code>children(optional <i>string</i> name)</code> returns all child nodes; if name is provided, the nodes are filtered by the name
+
 
 +
=== <i>array</i> <code>children(optional <i>string</i> name)</code> ===
 +
returns all child nodes; if name is provided, the nodes are filtered by the name
 
  local data = [[
 
  local data = [[
 
   <papers>
 
   <papers>
Line 45: Line 57:
 
  log(parsed.children("book")[2].text) -- Quran
 
  log(parsed.children("book")[2].text) -- Quran
 
  log(parsed.children()[2].text) -- Automoto Revue
 
  log(parsed.children()[2].text) -- Automoto Revue
* <i>XmlAttribute/nil</i> <code>get(<i>string/number</i>...)</code> traverses the xml tree and filters the result for each argument:
+
 
** <i>string</i> descend into first node of given name
+
===<i>XmlAttribute/nil</i> <code>get(<i>string/number</i>...)</code> ===
** <i>number</i> descend into n-th node of name specified by previous argument
+
traverses the xml tree and filters the result for each argument:
 +
* <i>string</i> descend into first node of given name
 +
* <i>number</i> descend into n-th node of name specified by previous argument
 
  local data = [[
 
  local data = [[
 
  <root>
 
  <root>
Line 61: Line 75:
 
  local parsed = xml.parse(data)
 
  local parsed = xml.parse(data)
 
  log(parsed.get("papers",2,"book").text) -- Quran
 
  log(parsed.get("papers",2,"book").text) -- Quran
* <i>XmlElement/nil</i> <code>next</code> next sibling node of the same name
+
 
 +
=== <i>XmlElement/nil</i> <code>next</code> ===
 +
next sibling node of the same name
 
  local data = [[
 
  local data = [[
 
   <papers>
 
   <papers>
Line 71: Line 87:
 
  local parsed = xml.parse(data)
 
  local parsed = xml.parse(data)
 
  log(parsed.child("book").next.text) -- Quran
 
  log(parsed.child("book").next.text) -- Quran
* <i>string</i> <code>xml</code> inner xml of parent XmlElement
+
 
 +
=== <i>string</i> <code>xml</code> ===
 +
inner xml of parent XmlElement
 
  local data = [[
 
  local data = [[
 
   <papers>
 
   <papers>
Line 82: Line 100:
 
  log(parsed.child("book").xml)
 
  log(parsed.child("book").xml)
 
-- <book>Bible</book> <magazine>Automoto Revue</magazine> <book>Quran</book>
 
-- <book>Bible</book> <magazine>Automoto Revue</magazine> <book>Quran</book>
* <i>string</i> <code>innerXml</code> inner xml of parent XmlElement
+
 
 +
=== <i>string</i> <code>innerXml</code> ===
 +
inner xml of parent XmlElement
 
  local data = [[
 
  local data = [[
 
   <papers>
 
   <papers>
Line 93: Line 113:
 
  log(parsed.innerXml)
 
  log(parsed.innerXml)
 
-- <book>Bible</book> <magazine>Automoto Revue</magazine> <book>Quran</book>
 
-- <book>Bible</book> <magazine>Automoto Revue</magazine> <book>Quran</book>
* <i>XmlElement/nil</i> <code>parent</code> parent node
+
 
 +
=== <i>XmlElement/nil</i> <code>parent</code> ===
 +
parent node
 
  local data = [[
 
  local data = [[
 
   <papers>
 
   <papers>
Line 103: Line 125:
 
  local magazine = xml.parse(data).child("magazine")
 
  local magazine = xml.parse(data).child("magazine")
 
  log(magazine.parent.name) -- papers
 
  log(magazine.parent.name) -- papers
* <i>XmlElement/nil</i> <code>root</code> root node (nil for root)
+
 
 +
=== <i>XmlElement/nil</i> <code>root</code> ===
 +
root node (nil for root)

Revision as of 10:59, 22 April 2017

Parses xml string.

XmlElement xml.parse(xmlstring)

Parameters

  • string xmlstring string to parse

Return value

XmlElement

Consecutive spaces are trimmed to single space.

XmlElement

string name

name of the node

log(xml.parse("<note>something</note>").name) -- note

string text

text content of the node (excluding the contents of child nodes). To read all content as a text, use .innerXml (see below)

local data = xml.parse("<note>something <big>really</big> good</note>")
log(data.text) -- something good

string/nil attr(string name)

returns the value of a single attribute; if the attribute doesn't exists, ruturns nil

local data = xml.parse('<note from="ann" to="bob">hello</note>')
log(data.attr("from")) -- ann

table attr()

returns all attributes as a table

local data = xml.parse('<note from="ann" to="bob">hello</note>')
local attrs = data.attr()
logf("%s -> %s", attrs.from, attrs["to"]) -- ann -> bob

XmlElement child(string name)

returns first child node with given name

local data = [[
  <papers>
    <book>Bible</book>
    <magazine>Automoto Revue</magazine>
    <book>Quran</book>
  </papers>
]]
local parsed = xml.parse(data)
log(parsed.child("book").text) -- Bible

array children(optional string name)

returns all child nodes; if name is provided, the nodes are filtered by the name

local data = [[
  <papers>
    <book>Bible</book>
    <magazine>Automoto Revue</magazine>
    <book>Quran</book>
  </papers>
]]
local parsed = xml.parse(data)
log(parsed.children("book")[2].text) -- Quran
log(parsed.children()[2].text) -- Automoto Revue

XmlAttribute/nil get(string/number...)

traverses the xml tree and filters the result for each argument:

  • string descend into first node of given name
  • number descend into n-th node of name specified by previous argument
local data = [[
<root>
  <papers>
    <book>Bible</book>
  </papers>
  <papers>
    <magazine>Automoto Revue</magazine>
    <book>Quran</book>
  </papers>
</root>
]]
local parsed = xml.parse(data)
log(parsed.get("papers",2,"book").text) -- Quran

XmlElement/nil next

next sibling node of the same name

local data = [[
  <papers>
    <book>Bible</book>
    <magazine>Automoto Revue</magazine>
    <book>Quran</book>
  </papers>
]]
local parsed = xml.parse(data)
log(parsed.child("book").next.text) -- Quran

string xml

inner xml of parent XmlElement

local data = [[
  <papers>
    <book>Bible</book>
    <magazine>Automoto Revue</magazine>
    <book>Quran</book>
  </papers>
]]
local parsed = xml.parse(data)
log(parsed.child("book").xml)

-- <book>Bible</book> <magazine>Automoto Revue</magazine> <book>Quran</book>

string innerXml

inner xml of parent XmlElement

local data = [[
  <papers>
    <book>Bible</book>
    <magazine>Automoto Revue</magazine>
    <book>Quran</book>
  </papers>
]]
local parsed = xml.parse(data)
log(parsed.innerXml)

-- <book>Bible</book> <magazine>Automoto Revue</magazine> <book>Quran</book>

XmlElement/nil parent

parent node

local data = [[
  <papers>
    <book>Bible</book>
    <magazine>Automoto Revue</magazine>
    <bool>Quran</bool>
  </papers>
]]
local magazine = xml.parse(data).child("magazine")
log(magazine.parent.name) -- papers

XmlElement/nil root

root node (nil for root)