I'm trying to build an XML structure using the internal data types from BaseX from Clojure.

(defn basex-elem [token-name dict]
  (let [elem (org.basex.query.item.FElem.
                (org.basex.query.item.QNm. token-name))]
    (for [[k v] dict]
        (println "THIS IS REACHED")
        (let [k-name (org.basex.query.item.QNm. (.getName k))
              k-attr (org.basex.query.item.FAttr.
                        org.basex.util.Token/token v))]
          (.add elem k-attr))))

When using this to cry to create an element, "THIS IS REACHED" is never printed:

(def test-elem (basex-elem "element-name" {:key1 "value1", :key2 "value2"}))
; => #'user/test-elem 

And thus the value comes back without any attributes:

; => #<FElem <element-name/>>

But adding attributes works otherwise.

(.add test-elem
        (org.basex.query.item.QNm. "foo")
        (org.basex.util.Token/token "bar")))
; => #<FElem <element-name foo="bar"/>>

Thus, presumably I'm doing something wrong with the loop. Any pointers?

1 Answers

sw1nn On Best Solutions

for is not a loop construct in clojure, rather it's a list comprehension and produces a lazy sequence.

Use doseq instead when side effects are intended.