Difference between revisions of "Function xml.parse()"
Jump to navigation
Jump to search
string
string
string/nil
table
XmlElement/nil
array
XmlElement/nil
XmlElement/nil
string
string
XmlElement/nil
XmlElement/nil
(→string xml) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:function xml.check()}} | {{DISPLAYTITLE:function xml.check()}} | ||
Parses xml string. | Parses xml string. | ||
− | <i>XmlElement</i> <b>xml.parse</b>(<code> | + | <i>XmlElement</i> <b>xml.parse</b>(<i>string</i> <code>xml</code>) |
=== Parameters === | === Parameters === | ||
− | * <i>string</i> <code> | + | * <i>string</i> <code>xml</code> string to parse |
=== Return value === | === Return value === | ||
Line 33: | Line 33: | ||
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> === | + | === <i>XmlElement/nil</i> <code>child(<i>string</i> name)</code> === |
− | returns first child node with given name | + | returns first child node with given name, nil if no such element is found |
local data = [[ | local data = [[ | ||
<papers> | <papers> | ||
Line 58: | Line 58: | ||
log(parsed.children()[2].text) -- Automoto Revue | log(parsed.children()[2].text) -- Automoto Revue | ||
− | ===<i> | + | ===<i>XmlElement/nil</i> <code>get(<i>string/number</i>...)</code> === |
traverses the xml tree and filters the result for each argument: | traverses the xml tree and filters the result for each argument: | ||
* <i>string</i> descend into first node of given name | * <i>string</i> descend into first node of given name | ||
Line 112: | Line 112: | ||
local parsed = xml.parse(data) | local parsed = xml.parse(data) | ||
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> === | === <i>XmlElement/nil</i> <code>parent</code> === |
Latest revision as of 18:20, 5 May 2017
Parses xml string.
XmlElement xml.parse(string xml
)
Contents
- 1 Parameters
- 2 Return value
- 3 XmlElement
- 3.1 string name
- 3.2 string text
- 3.3 string/nil attr(string name)
- 3.4 table attr()
- 3.5 XmlElement/nil child(string name)
- 3.6 array children(optional string name)
- 3.7 XmlElement/nil get(string/number...)
- 3.8 XmlElement/nil next
- 3.9 string xml
- 3.10 string innerXml
- 3.11 XmlElement/nil parent
- 3.12 XmlElement/nil root
Parameters
- string
xml
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/nil child(string name)
returns first child node with given name, nil if no such element is found
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
XmlElement/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)