Insertions into Zipper trees on XML files in Clojure

前端 未结 2 1678
情话喂你
情话喂你 2021-01-04 20:27

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?

2条回答
  •  误落风尘
    2021-01-04 21:01

    In create-item you mistyped :contents for :content and you should prefer vectors to lists for literals.

    (I was going to make a more comprehensive answer but Michal as already written a pretty good one.)

    An alternative to zip-filter is Enlive:

    (require '[net.cgrand.enlive-html :as e]) ;' <- fix SO colorizer
    
    (def db (ref (-> "itemdb.xml" java.io.File. e/xml-resource))
    
    (defn create-item [name desc]
      {:tag :item
       :attrs {:id "3"}
       :content [{:tag :name :attrs {} :content [name]}
                 {:tag :desc :attrs {} :content [desc]}]})
    
    (def fred-item (create-item "Fred" "Green-haired astrophysicist."))
    
    (dosync (alter db (e/transformation [:itemlist] (e/append fred-item))))
    

提交回复
热议问题