我找不到任何关于如何解析xml文档和访问元素的信息。
我发现了两种解析xml文档的方法
(clojure.zip/xml-zip (clojure.xml/parse file))
和
(parse-seq file)
但是我似乎可以找到任何关于如何处理产生的结构的信息?
关于如何查询结果,源文件的引用是zip-query.clj,但似乎也没有。
发布于 2009-07-28 17:36:46
假设您要在文件中解析以下xml:
<high-node>
<low-node>my text</low-node>
</high-node>
加载clojure.xml
:
user=> (use 'clojure.xml)
解析时,xml将具有以下结构:
{:tag :high-node, :attrs nil, :content [{:tag :low-node, :attrs nil, :content ["my text"]}]}
然后,您可以对文件的内容进行排序,以获得low-node
的内容
user=> (for [x (xml-seq
(parse (java.io.File. file)))
:when (= :low-node (:tag x))]
(first (:content x)))
("my text")
类似地,如果您希望访问低节点上的整个信息列表,则可以将:when
谓词更改为(= (: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"]})
这是因为关键字可以作为函数运行。请参阅Questions about lists and other stuff in Clojure和Data Structures: Keywords
https://stackoverflow.com/questions/1194044
复制相似问题