Xml
import "std/data" (included in import "std") · namespace std.data
XML read into a tree of elements and text (XmlNode), queried by paths of child names (find("entry/title"), * for any), and written back out, for feeds, sitemaps, SOAP and SAML replies and office files' XML parts. Entities and character references are decoded and CDATA kept as text. A document with a DTD (<!DOCTYPE>) is refused: DTDs are how XML entity attacks (XXE, "billion laughs") get in. Names keep their namespace prefix; localName() drops it, and child/find match either.
grav
import "std"
fn main() {
let feed = Xml.parse("<feed><entry id=\"1\"><title>Fish & chips</title></entry><entry id=\"2\"><title>Tea</title></entry></feed>")
for (e in feed.findAll("entry")) { print("${e.attr("id")}: ${e.find("title").text()}") }
let map = XmlNode.element("urlset")
map.add(XmlNode.element("url").add(XmlNode.element("loc").addText("https://example.com/?a=1&b=2")))
print(map.toString())
}1: Fish & chips
2: Tea
<urlset><url><loc>https://example.com/?a=1&b=2</loc></url></urlset>class XmlNode
An element (with a name, attributes and children) or a piece of text.
Constructors
new XmlNode(kind: string)
Static members
static fn element(name: string) -> XmlNodestatic fn textNode(text: string) -> XmlNode
Fields
kind: string— "element" or "text".name: string = ""— An element's name as written, prefix included ("soap:Body").value: string = ""— A text node's text (entities decoded).attrNames: Array<string> = new Array<string>()attrValues: Array<string> = new Array<string>()childNodes: Array<XmlNode> = new Array<XmlNode>()
Methods
fn isElement() -> boolfn localName() -> string— The name without its namespace prefix ("Body" for "soap:Body").fn attr(name: string, fallback: string = "") -> string— An attribute's value, orfallback.fn hasAttr(name: string) -> boolfn setAttr(name: string, value: string) -> XmlNode— Sets an attribute (replacing one of the same name).fn add(child: XmlNode) -> XmlNodefn addText(text: string) -> XmlNodefn children(name: string = "") -> Array<XmlNode>— The child elements (with this name, matching the name or local name; all with "").fn child(name: string) -> XmlNode— The first child element with this name, or null.fn text() -> string— All the text inside (descendants' too), joined.fn find(path: string) -> XmlNode— The first element at a path of child names ("entry/title", "*" for any), or null.fn findAll(path: string) -> Array<XmlNode>— Every element at a path of child names.fn toString(indent: bool = false) -> string— The node as XML (withindent, each element on its own line).
class Xml
Static members
static fn parse(text: string) -> XmlNode— The document's root element; ValueError (with the line) when it is not well-formed.static fn escape(s: string) -> string— Text with &, < and > escaped (for element content).static fn escapeAttr(s: string) -> string— Text escaped for an attribute value in double quotes.
class XmlParser
Constructors
new XmlParser(text: string)
Methods
fn document() -> XmlNode