March Docs

Xml

Non-validating XML parser (pure March, no FFI, namespace-unaware).

Supports: elements, attributes (double/single-quoted), text nodes, CDATA sections, comments, processing instructions, XML declaration, DOCTYPE declarations (skipped), entity references (< > & " '), self-closing tags.

Public API: Xml.parse(src) : Result(XmlDoc, XmlError) Xml.parse_exn(src) : XmlDoc (panics on error) Xml.to_string(doc) : String Xml.to_string_pretty(doc) : String Xml.root(doc) : XmlNode Xml.tag(node) : Option(String) Xml.text(node) : Option(String) Xml.attr(node, name) : Option(String) Xml.children(node) : List(XmlNode) Xml.elements(node) : List(XmlNode) Xml.find(node, path) : Option(XmlNode) Xml.find_all(node, path) : List(XmlNode) Xml.elem(tag, attrs, kids) : XmlNode Xml.text_node(s) : XmlNode

Types

typeXmlNodeXmlNode#
typeXmlDocXmlDoc = XmlDoc(Option(String), XmlNode)#
typeXmlErrorXmlError = XmlError(String, Int, Int)#
typeXmlFrameXmlFrame = XmlFrame(String, List((String, String)), List(XmlNode))#

Functions

fnattrattr(node : XmlNode, name : String) : Option(String)#

Get the value of an attribute by name, or None if absent.

fnchildrenchildren(node : XmlNode) : List(XmlNode)#

Get all child nodes of an element (text, elements, comments, etc.).

fnelemelem(tag_name : String, attrs : List((String, String)), kids : List(XmlNode)) : XmlNode#

Construct an element node.

fnelementselements(node : XmlNode) : List(XmlNode)#

Get only element-type children (filters out text, comments, etc.).

fnfindfind(node : XmlNode, path : String) : Option(XmlNode)#

Navigate to the first node matching a slash-delimited tag path.

    Xml.find(root, "item/name")  -- finds <item><name>...</name></item>
fnfind_allfind_all(node : XmlNode, path : String) : List(XmlNode)#

Find all element children at the last segment of a slash-delimited path.

    Xml.find_all(root, "items/item")  -- all <item> inside <items>
fnparseparse(src : String) : Result(XmlDoc, XmlError)#

Parse an XML string. Returns Ok(XmlDoc) on success or Err(XmlError) on failure. This is a non-validating parser: it does not check DTDs or enforce constraints beyond well-formedness.

    Xml.parse("<root><item>hello</item></root>")
    -- Ok(XmlDoc(None, Element("root", [], [Element("item", [], [Text("hello")])])))
fnparse_exnparse_exn(src : String) : XmlDoc#

Parse XML, panicking on error.

fnrootroot(xdoc : XmlDoc) : XmlNode#

Get the root element of a document.

fntagtag(node : XmlNode) : Option(String)#

Get the tag name of an element node, or None for non-element nodes.

fntexttext(node : XmlNode) : Option(String)#

Get the concatenated direct text content of a node, or None if no text children.

fntext_nodetext_node(s : String) : XmlNode#

Construct a text node.

fnto_stringto_string(xdoc : XmlDoc) : String#

Serialize an XmlDoc to a compact XML string.

fnto_string_prettyto_string_pretty(xdoc : XmlDoc) : String#

Serialize an XmlDoc to a pretty-printed XML string.