Clojure XML 解析 [英] Clojure XML Parsing

查看:22
本文介绍了Clojure XML 解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I have found two ways to parse the xml document

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

(parse-seq file)

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

but i can seem to find any info on how to process the resulting structure?

源文件是指关于如何查询结果的 zip-query.clj,但似乎也没有.

Source file's refers to zip-query.clj on how to query the result but that seems to missing too.

推荐答案

假设您的文件中有以下 xml 需要解析:

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

<high-node>
   <low-node>my text</low-node>
</high-node>

你加载clojure.xml:

user=> (use 'clojure.xml)

解析后,xml将具有以下结构:

when parsed, the xml will have the following structure:

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

然后你可以对文件的内容进行seq得到low-node的内容:

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")

同样,如果你想访问整个低节点信息列表,你可以将 :when 谓词更改为 (= (:high-node (:tagx))):

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"]})

这是可行的,因为关键字可以作为函数运行.请参阅有关 Clojure 中的列表和其他内容的问题数据结构:关键字

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

这篇关于Clojure XML 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆