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

From wiki.netio-products.com
Jump to navigation Jump to search
(Založena nová stránka s textem „{{DISPLAYTITLE:function xml.check()}} Parses xml string. <i>XmlElement</i> <b>xml.parse</b>(<code>xmlstring</code>) === Parameters === * <i>string</i> <c…“)
 
Line 34: Line 34:
 
  local parsed = xml.parse(data)
 
  local parsed = xml.parse(data)
 
  log(parsed.child("book").text) -- Bible
 
  log(parsed.child("book").text) -- Bible
 
=== Usage ===
 
local badnode = "<note>".."3<5".."</note>"
 
local goodnode = "<note>"..xml.escape("3<5").."</note>"
 
log(tostring(xml.check(badnode))) -- false
 
log(tostring(xml.check(goodnode))) -- true
 
 
=== See also ===
 
* [[Function xml.escape()|xml.escape()]] to xml-escape string
 
  
 
__NOTOC__
 
__NOTOC__

Revision as of 17:40, 21 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