API for clojure.data.xml - Lazy XML parsing 0.1.0 (in development)

by Chris Houser

Full namespace name: clojure.data.xml

Overview

Functions to parse XML into lazy sequences and lazy trees and
emit these as text.

Public Variables and Functions



aggregate-xmlns

function
Usage: (aggregate-xmlns xml)
Put all occurring xmlns into the root
Source


alias-ns

function
Usage: (alias-ns & {:as alias-nss})
Define a clojure namespace alias for shortened keyword and symbol namespaces.
 Similar to clojure.core/alias, but if namespace doesn't exist, it is created.

 ## Example
 (declare-ns :xml.dav "DAV:")
 (alias-ns :D :xml.dav)
{:tag ::D/propfind :content []}
Source


canonical-name

function
Usage: (canonical-name n)
Put (q)name into canonical form as per ns-env
Source


cdata

function
Usage: (cdata content)
Create a CData node
Source


declare-ns

function
Usage: (declare-ns & {:as cljns-xmlnss})
Define mappings in the global keyword-ns -> qname-uri mapping table.
Arguments are pairs of ns-name - qname-uri
ns-name must be a string, symbol, keyword or clojure namespace. The canonical form is string.
ns-uri must be a string
Source


element

function
Usage: (element tag)
       (element tag attrs)
       (element tag attrs & content)
Create an xml Element from content varargs
Source


element*

function
Usage: (element* tag attrs content meta)
       (element* tag attrs content)
Create an xml element from a content collection and optional metadata
Source


element-nss

function
Usage: (element-nss {:keys [attrs], :as element})
Get xmlns environment from element
Source


emit

function
Usage: (emit e writer & {:as opts})
Prints the given Element tree as XML text to stream.
Options:
 :encoding <str>          Character encoding to use
Source


emit-str

function
Usage: (emit-str e & opts)
Emits the Element to String and returns it
Source


event-seq

function
Usage: (event-seq source {:as props})
Parses the XML InputSource source using a pull-parser. Returns
a lazy sequence of Event records.  Accepts key pairs
with XMLInputFactory options, see http://docs.oracle.com/javase/6/docs/api/javax/xml/stream/XMLInputFactory.html
and xml-input-factory-props for more information.
Defaults coalescing true and supporting-external-entities false.
:include-node? can be a set of #{:start-element :end-element :characters :comment}
Source


find-xmlns

function
Usage: (find-xmlns xml)
Find all xmlns occuring in a root
Source


indent

function
Usage: (indent e writer & opts)
Emits the XML and indents the result.  WARNING: this is slow
it will emit the XML and read it in again to indent it.  Intended for
debugging/testing only.
Source


indent-str

function
Usage: (indent-str e & opts)
Emits the XML and indents the result.  Writes the results to a String and returns it
Source


ns-uri

function
Usage: (ns-uri ns)
Look up xmlns uri to keyword namespace
Source


parse

function
Usage: (parse source & opts)
Parses the source, which can be an
InputStream or Reader, and returns a lazy tree of Element records. Accepts key pairs
with XMLInputFactory options, see http://docs.oracle.com/javase/6/docs/api/javax/xml/stream/XMLInputFactory.html
and xml-input-factory-props for more information. Defaults coalescing true.
Source


parse-str

function
Usage: (parse-str s & opts)
Parses the passed in string to Clojure data structures.  Accepts key pairs
with XMLInputFactory options, see http://docs.oracle.com/javase/6/docs/api/javax/xml/stream/XMLInputFactory.html
and xml-input-factory-props for more information. Defaults coalescing true.
Source


sexp-as-element

function
Usage: (sexp-as-element sexp)
Convert a single sexp into an Element
Source


sexps-as-fragment

function
Usage: (sexps-as-fragment)
       (sexps-as-fragment sexp)
       (sexps-as-fragment sexp & sexps)
Convert a compact prxml/hiccup-style data structure into the more formal
tag/attrs/content format. A seq of elements will be returned, which may
not be suitable for immediate use as there is no root element. See also
sexp-as-element.

The format is [:tag-name attr-map? content*]. Each vector opens a new tag;
seqs do not open new tags, and are just used for inserting groups of elements
into the parent tag. A bare keyword not in a vector creates an empty element.

To provide XML conversion for your own data types, extend the AsElements
protocol to them.
Source


uri-ns

function
Usage: (uri-ns uri)
Look up keyword namespace to xmlns uri
Source


xml-comment

function
Usage: (xml-comment content)
Create a Comment node
Source

clojure.data.xml.event

Data type for xml pull events

Types



CDataEvent

record

    Fields: [str]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map


CharsEvent

record

    Fields: [str]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map


CommentEvent

record

    Fields: [str]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map


EndElementEvent

record

    Fields: [tag]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map


StartElementEvent

record

    Fields: [tag attrs nss]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map

Public Variables and Functions



->CDataEvent

function
Usage: (->CDataEvent str)
Positional factory function for class clojure.data.xml.event.CDataEvent.
Source


->CharsEvent

function
Usage: (->CharsEvent str)
Positional factory function for class clojure.data.xml.event.CharsEvent.
Source


->CommentEvent

function
Usage: (->CommentEvent str)
Positional factory function for class clojure.data.xml.event.CommentEvent.
Source


->EndElementEvent

function
Usage: (->EndElementEvent tag)
Positional factory function for class clojure.data.xml.event.EndElementEvent.
Source


->StartElementEvent

function
Usage: (->StartElementEvent tag attrs nss)
Positional factory function for class clojure.data.xml.event.StartElementEvent.
Source


element-nss

function
Usage: (element-nss {:keys [attrs], :as element})
Get xmlns environment from element
Source


map->CDataEvent

function
Usage: (map->CDataEvent m#)
Factory function for class clojure.data.xml.event.CDataEvent, taking a map of keywords to field values.
Source


map->CharsEvent

function
Usage: (map->CharsEvent m#)
Factory function for class clojure.data.xml.event.CharsEvent, taking a map of keywords to field values.
Source


map->CommentEvent

function
Usage: (map->CommentEvent m#)
Factory function for class clojure.data.xml.event.CommentEvent, taking a map of keywords to field values.
Source


map->EndElementEvent

function
Usage: (map->EndElementEvent m#)
Factory function for class clojure.data.xml.event.EndElementEvent, taking a map of keywords to field values.
Source


map->StartElementEvent

function
Usage: (map->StartElementEvent m#)
Factory function for class clojure.data.xml.event.StartElementEvent, taking a map of keywords to field values.
Source

clojure.data.xml.impl

Shared private code for data.xml namespaces

Public Variables and Functions



export-api

macro
Usage: (export-api & names)
This creates vars, that take their (local) name, value and metadata from another var
Source


extend-protocol-fns

macro
Usage: (extend-protocol-fns proto & types+mmaps)
Helper to many types to a protocol with a method map, similar to extend
Source


static-case

macro
Usage: (static-case val & cases)
Variant of case where keys are evaluated at compile-time
Source

clojure.data.xml.jvm.emit

JVM implementation of the emitter details

Public Variables and Functions



write-document

function
Usage: (write-document swriter events opts)
Writes the given event seq as XML text to writer.
Options:
 :encoding <str>          Character encoding to use
Source

clojure.data.xml.jvm.parse





Public Variables and Functions



pull-seq

function
Usage: (pull-seq sreader include-node? ns-envs)
Creates a seq of events.  The XMLStreamConstants/SPACE clause below doesn't seem to
be triggered by the JDK StAX parser, but is by others.  Leaving in to be more complete.
Source

clojure.data.xml.name





Public Variables and Functions



*gen-prefix-counter*

dynamic var

    
Thread local counter for a single document
Source


alias-ns

function
Usage: (alias-ns & {:as alias-nss})
Define a clojure namespace alias for shortened keyword and symbol namespaces.
 Similar to clojure.core/alias, but if namespace doesn't exist, it is created.

 ## Example
 (declare-ns :xml.dav "DAV:")
 (alias-ns :D :xml.dav)
{:tag ::D/propfind :content []}
Source


declare-ns

function
Usage: (declare-ns & {:as cljns-xmlnss})
Define mappings in the global keyword-ns -> qname-uri mapping table.
Arguments are pairs of ns-name - qname-uri
ns-name must be a string, symbol, keyword or clojure namespace. The canonical form is string.
ns-uri must be a string
Source


gen-prefix

function
Usage: (gen-prefix)
       (gen-prefix n)
Generates an xml prefix.
Zero-arity can only be called, when *gen-prefix-counter* is bound and will increment it.
Source


merge-nss

function
Usage: (merge-nss nss1 nss2)
Merge two attribute sets, deleting assignments of empty-string
Source


ns-uri

function
Usage: (ns-uri ns)
Look up xmlns uri to keyword namespace
Source


separate-xmlns

function
Usage: (separate-xmlns attrs cont)
Call cont with two args: attributes and xmlns attributes
Source


uri-ns

function
Usage: (uri-ns uri)
Look up keyword namespace to xmlns uri
Source


xmlns-attr?

function
Usage: (xmlns-attr? qn)
Is this qname an xmlns declaration?
Source

clojure.data.xml.node

Data types for xml nodes: Element, CData and Comment

Types



CData

record

    Fields: [content]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map


Comment

record

    Fields: [content]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map


Element

record

    Fields: [tag attrs content]
Protocols:
Interfaces: clojure.lang.IHashEq, clojure.lang.IKeywordLookup, clojure.lang.ILookup, clojure.lang.IObj, clojure.lang.IPersistentMap, java.io.Serializable, java.util.Map

Public Variables and Functions



->CData

function
Usage: (->CData content)
Positional factory function for class clojure.data.xml.node.CData.
Source


->Comment

function
Usage: (->Comment content)
Positional factory function for class clojure.data.xml.node.Comment.
Source


->Element

function
Usage: (->Element tag attrs content)
Positional factory function for class clojure.data.xml.node.Element.
Source


cdata

function
Usage: (cdata content)
Create a CData node
Source


element

function
Usage: (element tag)
       (element tag attrs)
       (element tag attrs & content)
Create an xml Element from content varargs
Source


element*

function
Usage: (element* tag attrs content meta)
       (element* tag attrs content)
Create an xml element from a content collection and optional metadata
Source


map->CData

function
Usage: (map->CData m#)
Factory function for class clojure.data.xml.node.CData, taking a map of keywords to field values.
Source


map->Comment

function
Usage: (map->Comment m#)
Factory function for class clojure.data.xml.node.Comment, taking a map of keywords to field values.
Source


map->Element

function
Usage: (map->Element m#)
Factory function for class clojure.data.xml.node.Element, taking a map of keywords to field values.
Source


xml-comment

function
Usage: (xml-comment content)
Create a Comment node
Source

clojure.data.xml.process





Public Variables and Functions



aggregate-xmlns

function
Usage: (aggregate-xmlns xml)
Put all occurring xmlns into the root
Source


find-xmlns

function
Usage: (find-xmlns xml)
Find all xmlns occuring in a root
Source

clojure.data.xml.protocols





Protocols



AsElements

Protocol

    Known implementations: clojure.lang.IPersistentVector, clojure.lang.ISeq, clojure.lang.Keyword, java.lang.String, nil, Object
    

as-elements

function
Usage: (as-elements expr)
Return a seq of elements represented by an expression.
Source


AsQName

Protocol

    Known implementations: clojure.lang.Keyword, java.lang.String, javax.xml.namespace.QName
    

qname-local

function
Usage: (qname-local qname)
Get the name for this qname

qname-uri

function
Usage: (qname-uri qname)
Get the namespace uri for this qname
Source


EventGeneration

Protocol
Protocol for generating new events based on element type
Known implementations: clojure.data.xml.event.CDataEvent, clojure.data.xml.event.CDataEvent, clojure.data.xml.event.CharsEvent, clojure.data.xml.event.CharsEvent, clojure.data.xml.event.CommentEvent, clojure.data.xml.event.CommentEvent, clojure.data.xml.event.EndElementEvent, clojure.data.xml.event.EndElementEvent, clojure.data.xml.event.StartElementEvent, clojure.data.xml.event.StartElementEvent, clojure.data.xml.node.CData, clojure.data.xml.node.Comment, clojure.data.xml.node.Element, clojure.lang.IPersistentMap, clojure.lang.Sequential, java.lang.Boolean, java.lang.Number, java.lang.String, nil

gen-event

function
Usage: (gen-event item)
Function to generate an event for e.

next-events

function
Usage: (next-events item next-items)
Returns the next set of events that should occur after e.  next-events are the
events that should be generated after this one is complete.
Source

clojure.data.xml.prxml





Public Variables and Functions



sexp-as-element

function
Usage: (sexp-as-element sexp)
Convert a single sexp into an Element
Source


sexps-as-fragment

function
Usage: (sexps-as-fragment)
       (sexps-as-fragment sexp)
       (sexps-as-fragment sexp & sexps)
Convert a compact prxml/hiccup-style data structure into the more formal
tag/attrs/content format. A seq of elements will be returned, which may
not be suitable for immediate use as there is no root element. See also
sexp-as-element.

The format is [:tag-name attr-map? content*]. Each vector opens a new tag;
seqs do not open new tags, and are just used for inserting groups of elements
into the parent tag. A bare keyword not in a vector creates an empty element.

To provide XML conversion for your own data types, extend the AsElements
protocol to them.
Source

clojure.data.xml.tree





Public Variables and Functions



event-tree

function
Usage: (event-tree events)
Returns a lazy tree of Element objects for the given seq of Event
objects. See source-seq and parse.
Source


flatten-elements

function
Usage: (flatten-elements elements)
Flatten a collection of elements to an event seq
Source


seq-tree

function
Usage: (seq-tree parent exit? node coll)
Takes a seq of events that logically represents
a tree by each event being one of: enter-sub-tree event,
exit-sub-tree event, or node event.

Returns a lazy sequence whose first element is a sequence of
sub-trees and whose remaining elements are events that are not
siblings or descendants of the initial event.

The given exit? function must return true for any exit-sub-tree
event.  parent must be a function of two arguments: the first is an
event, the second a sequence of nodes or subtrees that are children
of the event.  parent must return nil or false if the event is not
an enter-sub-tree event.  Any other return value will become
a sub-tree of the output tree and should normally contain in some
way the children passed as the second arg.  The node function is
called with a single event arg on every event that is neither parent
nor exit, and its return value will become a node of the output tree.

(seq-tree #(when (= %1 :<) (vector %2)) #{:>} str
          [1 2 :< 3 :< 4 :> :> 5 :> 6])
;=> (("1" "2" [("3" [("4")])] "5") 6)
Source
Logo & site design by Tom Hickey.
Clojure auto-documentation system by Tom Faulhaber.