Note that there are some explanatory texts on larger screens.

plurals
  1. POInsertions into Zipper trees on XML files in Clojure
    primarykey
    data
    text
    <p>I'm confused as how to idiomatically change a xml tree accessed through clojure.contrib's zip-filter.xml. Should be trying to do this at all, or is there a better way?</p> <p>Say that I have some dummy xml file "itemdb.xml" like this:</p> <pre><code>&lt;itemlist&gt; &lt;item id="1"&gt; &lt;name&gt;John&lt;/name&gt; &lt;desc&gt;Works near here.&lt;/desc&gt; &lt;/item&gt; &lt;item id="2"&gt; &lt;name&gt;Sally&lt;/name&gt; &lt;desc&gt;Owner of pet store.&lt;/desc&gt; &lt;/item&gt; &lt;/itemlist&gt; </code></pre> <p>And I have some code:</p> <pre><code>(require '[clojure.zip :as zip] '[clojure.contrib.duck-streams :as ds] '[clojure.contrib.lazy-xml :as lxml] '[clojure.contrib.zip-filter.xml :as zf]) (def db (ref (zip/xml-zip (lxml/parse-trim (java.io.File. "itemdb.xml"))))) ;; Test that we can traverse and parse. (doall (map #(print (format "%10s: %s\n" (apply str (zf/xml-&gt; % :name zf/text)) (apply str (zf/xml-&gt; % :desc zf/text)))) (zf/xml-&gt; @db :item))) ;; I assume something like this is needed to make the xml tags (defn create-item [name desc] {:tag :item :attrs {:id "3"} :contents (list {:tag :name :attrs {} :contents (list name)} {:tag :desc :attrs {} :contents (list desc)})}) (def fred-item (create-item "Fred" "Green-haired astrophysicist.")) ;; This disturbs the structure somehow (defn append-item [xmldb item] (zip/insert-right (-&gt; xmldb zip/down zip/rightmost) item)) ;; I want to do something more like this (defn append-item2 [xmldb item] (zip/insert-right (zip/rightmost (zf/xml-&gt; xmldb :item)) item)) (dosync (alter db append-item2 fred-item)) ;; Save this simple xml file with some added stuff. (ds/spit "appended-itemdb.xml" (with-out-str (lxml/emit (zip/root @db) :pad true))) </code></pre> <p>I am unclear about how to use the clojure.zip functions appropriately in this case, and how that interacts with zip-filter.</p> <p>If you spot anything particularly weird in this small example, please point it out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload