Xml
Object for xml manipulations.
Static methods for creating Xml object:
Lib.Sys.Xml.createCData(data) |
Creates a node of the given type. |
data - node content |
Lib.Sys.Xml.createComment(data) |
Creates a node of the given type. |
data - node content |
Lib.Sys.Xml.createDocType(data) |
Creates a node of the given type. |
data - node content |
Lib.Sys.Xml.createDocument() |
Creates a node of the given type. |
|
Lib.Sys.Xml.createElement(name) |
Creates a node of the given type. |
name - element name |
Lib.Sys.Xml.createPCData(data) |
Creates a node of the given type. |
data - node content |
Lib.Sys.Xml.createProcessingInstruction(data) |
Creates a node of the given type. |
data - node content |
Lib.Sys.Xml.parse(str) |
Parses the string str into an Xml document. |
str - xml text |
Static properties:
Lib.Sys.Xml.XmlType.Element |
XML element type. |
read-only |
Lib.Sys.Xml.XmlType.PCData |
XML parsed character data type. |
read-only |
Lib.Sys.Xml.XmlType.CData |
XML character data type. |
read-only |
Lib.Sys.Xml.XmlType.Comment |
XML comment type. |
read-only |
Lib.Sys.Xml.XmlType.DocType |
XML doctype element type. |
read-only |
Lib.Sys.Xml.XmlType.ProcessingInstruction |
XML processing instruction type. |
read-only |
Lib.Sys.Xml.XmlType.Document |
XML document type. |
read-only |
Created Xml object has properties:
nodeName |
Returns the node name of an Element. |
|
nodeType |
Returns the type of the Xml Node. This should be used before accessing other functions since some might raise an exception if the node type is not correct. |
read-only |
nodeValue |
Returns the node value. Only works if the Xml node is not an Element or a Document. |
|
parent |
Returns the parent object in the Xml hierarchy. The parent can be nil, an Element or a Document. |
read-only |
Created Xml object has methods:
addChild(x) |
Adds a child node x to the Document or Element. A child node can only be inside one given parent node, which is indicated by the parent property. If the child is already inside this Document or Element, it will be moved to the last position among the Document or Element's children. If the child node was previously inside a different node, it will be moved to this Document or Element. |
x - Xml object |
Returns an Iterator on all the attribute names. |
|
|
elements() |
Returns an Iterator of all child nodes which are Elements. Only works if the current node is an Element or a Document. |
|
elementsNamed(name) |
Returns an Iterator of all child nodes which are Elements with the given nodeName name. Only works if the current node is an Element or a Document. |
name - nodeName string |
exists(att) |
Tells if the Element node has a given attribute att. Attributes are case-sensitive. |
att - attribute string |
Returns the first child node. |
|
|
Returns the first child node which is an Element. |
|
|
get(att) |
Get the given attribute att of an Element node. Returns nil if not found. Attributes are case-sensitive. |
att - attribute string |
insertChild(x, pos) |
Inserts a child x at the given position pos among the other childs. A child node can only be inside one given parent node, which is indicated by the [parent] property. If the child is already inside this Document or Element, it will be moved to the new position among the Document or Element's children. If the child node was previously inside a different node, it will be moved to this Document or Element. |
x - Xml object pos - position index |
iterator() |
Returns an Iterator of all child nodes. Only works if the current node is an Element or a Document. |
|
remove(att) |
Removes an attribute att for an Element node. Attributes are case-sensitive. |
att - attribute string |
removeChild(x) |
Removes a child x from the Document or Element. Returns true if the child was successfuly removed. |
x - Xml object |
set(att, value) |
Set the given attribute att value for an Element node. Attributes are case-sensitive. |
att - attribute string value - attribute att value string |
toString() |
Returns a String representation of the Xml node. |
|
Examples:
function getNodeTypeName(nodeType)
if nodeType == Lib.Sys.Xml.XmlType.Element then return "Element"
elseif nodeType == Lib.Sys.Xml.XmlType.PCData then return "PCData"
elseif nodeType == Lib.Sys.Xml.XmlType.CData then return "CData"
elseif nodeType == Lib.Sys.Xml.XmlType.Comment then return "Comment"
elseif nodeType == Lib.Sys.Xml.XmlType.DocType then return "DocType"
elseif nodeType == Lib.Sys.Xml.XmlType.ProcessingInstruction then return "ProcessingInstruction"
elseif nodeType == Lib.Sys.Xml.XmlType.Document then return "Document"
else return "Unknown" end
end
xml = Lib.Sys.Xml.parse("<html><body><a href='#test'>test1</a><div><![CDATA[test2]]></div></body></html>")
html = xml.firstElement()
print(html.nodeName.." "..getNodeTypeName(html.nodeType))
body = html.elements().next()
print(body.nodeName.." "..getNodeTypeName(html.nodeType))
bodyElements = body.elements()
while bodyElements.hasNext() do
element = bodyElements.next()
print(element.nodeName.." "..getNodeTypeName(element.nodeType))
if element.exists("href") then
attributes = element.attributes()
while attributes.hasNext() do
print(attributes.next())
end
else
element.set("width", "10")
end
children = element.iterator()
while children.hasNext() do
child = children.next()
print(child.nodeValue.." "..getNodeTypeName(child.nodeType))
end
end
body.addChild(Lib.Sys.Xml.createElement("br"))
xmlText = xml.toString()
print(xmlText)
Created with the Personal Edition of HelpNDoc: Easily create Help documents