首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用clojure.data.xml进行XML解析。如何在"parse“中省略"\n”项:content

使用clojure.data.xml进行XML解析。如何在"parse“中省略"\n”项:content
EN

Stack Overflow用户
提问于 2019-04-24 05:57:14
回答 1查看 361关注 0票数 0

首先,我将XML文件解析为

代码语言:javascript
运行
复制
(def xtest (slurp "./resources/smallXMLTest.xml"))
(def way1 (clojure.xml/parse))
(:content way1)

并且在:content哈希图中没有任何"\n“项。

但是当我像这样解析XML时,在clojure.data.xml的帮助下

代码语言:javascript
运行
复制
(def fr
 (-> filename
     io/file
     io/input-stream
     io/reader))

(def fileAsStream (fr XMLfilePath))

(def way2 (clojure.data.xml/parse fileAsStream))

然后我在way2变量中的每个非叶:content元素中,在每对内部XMLElements之间获得了"\n“字符串:(

有没有办法避免这些"\n“字符串?

EN

回答 1

Stack Overflow用户

发布于 2019-04-24 08:20:01

我最近向the Tupelo library添加了两个解析器,一个基于clojure.data.xml,另一个基于tagsoup。在这两种情况下,我都默认删除了空格节点。这是the operative function

代码语言:javascript
运行
复制
(defn enlive-remove-whitespace
  "Removes whilespace strings from Enlive data :content vectors."
  [item]
  (if (and (map? item) ; Enlive data parsed from XML may has raw strings (esp. whitespace) embedded in it
        (contains-key? item :tag)) ; when parsing html, may get non-enlive nodes like {:type :comment, :data "..."}
    (let [content-new (cond-it-> (:content item)
                        (or (nil? it) (empty? it)) []
                        :then (drop-if (fn [arg]
                                         (and (string? arg)
                                           (ts/whitespace? arg))) it)
                        :then (mapv enlive-remove-whitespace it))]
      (glue item {:content content-new}))
    item))

对于tupelo.parse.xml,它的用法如下所示

代码语言:javascript
运行
复制
(s/defn parse       ; #todo fix docstring
  ([xml-input] (parse xml-input sax-parse-fn))
  ([xml-input parse-fn]
    (enlive-remove-whitespace
      (enlive-normalize
        (parse-raw xml-input parse-fn)))))

因此,如果您不想对生成的Enlive格式的数据进行规范化或空格修剪,则可以使用the function parse-raw

tupelo.parse.tagsoup名称空间中提供了类似的choices for parse and parse-raw

您可以查看用法示例in the test ns

代码语言:javascript
运行
复制
(def xml-str "<foo>
                <name>John</name>
                <address>1 hacker way</address>
                <phone></phone>
                <school>
                    <name>Joe</name>
                    <state>CA</state>
                    <type>FOOBAR</type>
                </school>
                <college>
                    <name>mit</name>
                    <address></address>
                    <state>Denial</state>
                </college>
              </foo> ")

(def enlive-tree-normalized-nonblank
  {:tag     :foo,
   :attrs   {},
   :content [{:tag :name, :attrs {}, :content ["John"]}
             {:tag :address, :attrs {}, :content ["1 hacker way"]}
             {:tag :phone, :attrs {}, :content []}
             {:tag     :school,
              :attrs   {},
              :content [{:tag :name, :attrs {}, :content ["Joe"]}
                        {:tag :state, :attrs {}, :content ["CA"]}
                        {:tag :type, :attrs {}, :content ["FOOBAR"]}]}
             {:tag     :college,
              :attrs   {},
              :content [{:tag :name, :attrs {}, :content ["mit"]}
                        {:tag :address, :attrs {}, :content []}
                        {:tag :state, :attrs {}, :content ["Denial"]}]}]})

有结果

代码语言:javascript
运行
复制
(dotest  
  (let [xml-data              (xml/parse     (ts/string->stream xml-str))
        tagsoup-data          (tagsoup/parse (ts/string->stream xml-str))]
    (is= enlive-tree-normalized-nonblank xml-data)
    (is= enlive-tree-normalized-nonblank tagsoup-data) ))
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55819966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档