Dabscm Library Reference

(scm xml)

XML file reading and navigation

Overview

(scm xml) is a streaming (pull) XML reader. You open a reader over a file, string, or bytevector, then call xml-read to advance node by node, querying the current node's type, name, value, and attributes as you go. This keeps memory use low for large documents.

Common uses

(import (scm xml))

(define r (open-xml-string "<a x=\"1\"><b>hi</b></a>"))

(xml-read r)          ;; advance to the next node => #t (or #f at end)
(xml-node-type r)     ;; => element
(xml-name r)          ;; => "a"
(xml-attribute r "x") ;; => "1"

Drive it in a loop until xml-read returns #f. xml-value reads text content, xml-read-to skips ahead to a named element, and close-xml releases the reader. Open from a file or bytes with open-xml-file / open-xml-bytevector.

10 bindings

close-xml
Syntax: (close-xml reader)
Library: (scm core)
Description: Closes the given XML reader, releasing any underlying file or stream resources.
Example:
  (let ((r (open-xml-file "data.xml")))
    (close-xml r))
open-xml-bytevector
Syntax: (open-xml-bytevector bv)
Library: (scm xml)
Description: Opens the XML document encoded in the given bytevector and returns an XML reader for forward-only reading of XML nodes. The byte stream is decoded using the XML declaration's encoding, defaulting to UTF-8.
Example:
  (define r (open-xml-bytevector (string->utf8 "<a/>")))
  (xml-node-type r) => element
open-xml-file
Syntax: (open-xml-file filename)
Library: (scm core)
Description: Opens the named XML file and returns an XML reader object for forward-only reading of XML nodes.
Example:
  (define r (open-xml-file "data.xml"))
  (xml-node-type r) => node-type of first node
open-xml-string
Syntax: (open-xml-string source)
Library: (scm xml)
Description: Opens the given XML string and returns an XML reader object for forward-only reading of XML nodes.
Example:
  (define r (open-xml-string "<a/>"))
  (xml-node-type r) => element
xml-attribute
Syntax: (xml-attribute xml-reader name)
Library: (scm xml)
Description: Returns the value of the named attribute from the current element of xml-reader as a string, or #f if the attribute does not exist.
Example:
  (xml-attribute reader "id") => "42"
  (xml-attribute reader 'class) => #f
xml-name
Syntax: (xml-name xml-reader)
Library: (scm xml)
Description: Returns the qualified name of the current XML element or attribute node as a string.
Example:
  (xml-name reader) => "Customer"
  (xml-name reader) => "ns:Element"
xml-node-type
Syntax: (xml-node-type xml-reader)
Library: (scm xml)
Description: Returns a symbol identifying the type of the current XML node: element, end-element, text, cdata, comment, pi, xml-decl, doc, doc-type, entity-ref, or #f for unrecognized types.
Example:
  (xml-node-type reader) => element
  (xml-node-type reader) => text
xml-read
Syntax: (xml-read xml-reader)
Library: (scm xml)
Description: Advances xml-reader to the next node in the XML document. Returns #t if a node was read, or #f if the end of the document was reached.
Example:
  (xml-read reader) => #t
  (xml-read reader) => #f
xml-read-to
Syntax: (xml-read-to xml-reader name)
Library: (scm xml)
Description: Advances xml-reader forward until it reaches an element with the given name. Returns #t if found, or #f if the end of the document is reached first.
Example:
  (xml-read-to reader "Customer") => #t
  (xml-read-to reader 'Item) => #f
xml-value
Syntax: (xml-value xml-reader)
Library: (scm xml)
Description: Reads and returns the text content of the current element as a string, or #f if there is no value.
Example:
  (xml-value reader) => "John Doe"
  (xml-value reader) => #f