gpt4 book ai didi

xml - Clojure XML 解析

转载 作者:数据小太阳 更新时间:2023-10-29 01:37:02 26 4
gpt4 key购买 nike

我找不到任何关于如何解析 xml 文档和访问元素的信息。

我找到了两种解析xml文档的方法

(clojure.zip/xml-zip (clojure.xml/parse file))

(parse-seq file)

但我似乎可以找到任何关于如何处理结果结构的信息?

关于如何查询结果的源文件引用了 zip-query.clj,但似乎也丢失了。

最佳答案

假设您的文件中有以下要解析的 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"]}]}

然后您可以对文件的内容进行序列化以获取低节点的内容:

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 ClojureData Structures: Keywords

关于xml - Clojure XML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1194044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com