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

From wiki.netio-products.com
Jump to navigation Jump to search
(XmlElement Members)
(XmlElement Members)
Line 48: Line 48:
 
** <i>string</i> descend into first node of given name
 
** <i>string</i> descend into first node of given name
 
** <i>number</i> descend into n-th node of name specified by previous argument
 
** <i>number</i> 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
 +
* <i>XmlElement/nil</i> <code>next</code> 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
 +
* <i>string</i> <code>xml</code> 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>
 
__NOTOC__
 
__NOTOC__

Revision as of 10:43, 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 Members

  • 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)
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>