1

我是 clojure 的新手,需要解析一些 XML。其他 SO 问题将我指向data.zip(尽管大多数仍然指向旧的 clojure-contrib.zip)。我想做的就是收集一组标签,相当于//Foo. 但是,虽然我可以解析 xml,但我的所有匹配项都是空的。

出于想法,我决定将部分测试代码从 data.zip 复制到一个独立的 clj 文件中。当然这应该有效:

(ns test.xmltest
  (:require [clojure.xml :as xml]
            [clojure.zip :as zip]
            )
  (:use clojure.pprint)
  (:use clojure.data.zip.xml)

)

(defn parse-str [s]
    (zip/xml-zip (xml/parse (new org.xml.sax.InputSource
                            (new java.io.StringReader s)))))

(def atom1 (parse-str "<?xml version='1.0' encoding='UTF-8'?>
                      <feed xmlns='http://www.w3.org/2005/Atom'>
                        <id>tag:blogger.com,1999:blog-28403206</id>
                        <updated>2008-02-14T08:00:58.567-08:00</updated>
                        <title type='text'>n01senet</title>
                        <link rel='alternate' type='text/html' href='http://n01senet.blogspot.com/'/>
                        <entry>
                          <id>1</id>
                          <published>2008-02-13</published>
                          <title type='text'>clojure is the best lisp yet</title>
                          <author><name>Chouser</name></author>
                        </entry>
                        <entry>
                          <id>2</id>
                          <published>2008-02-07</published>
                          <title type='text'>experimenting with vnc</title>
                          <author><name>agriffis</name></author>
                        </entry>
                      </feed>
                      "))

(defn -main []
  (pprint atom1)
  (xml-> atom1 :title :text)
)

()然而,虽然漂亮的打印输出看起来不错,但无论我输入什么,实际匹配结果始终是。但是,我看不出这段代码和测试文件中的代码(第 42 行)有什么区别。但我一定是错过了什么。

我正在运行 clojure 1.4,leiningen 2,不应该做任何花哨的事情。

4

1 回答 1

1

才意识到问题。非常愚蠢的错字,最后一行应该是text,不是:text

于 2012-08-24T21:16:29.960 回答