Clojure XML Parsing

后端 未结 2 2050
忘了有多久
忘了有多久 2020-12-22 16:40

I can not find any info on how to parse xml documents and access elements.

I have found two ways to parse the xml document

(clojure.zip/xml-zip (cloj         


        
2条回答
  •  臣服心动
    2020-12-22 17:23

    Suppose you have the following xml to parse in your file:

    
       my text
    
    

    you load clojure.xml:

    user=> (use 'clojure.xml)
    

    when parsed, the xml will have the following structure:

    {:tag :high-node, :attrs nil, :content [{:tag :low-node, :attrs nil, :content ["my text"]}]}
    

    and then you can seq over the content of the file to get the content of the low-node:

    user=> (for [x (xml-seq 
                  (parse (java.io.File. file)))
                     :when (= :low-node (:tag x))]
             (first (:content x)))
    
    ("my text")
    

    Similarly, if you wanted to have access to the entire list of information on low-node, you would change the :when predicate to (= (:high-node (:tag x))):

    user=> (for [x (xml-seq 
                  (parse (java.io.File. file)))
                     :when (= :high-node (:tag x))]
             (first (:content x)))
    
    ({:tag :low-node, :attrs nil, :content ["my text"]})
    

    This works because the keywords can operate as functions. See Questions about lists and other stuff in Clojure and Data Structures: Keywords

提交回复
热议问题