I need a way to preserve comments when parsing xml from a file.
我需要一种方法来保存从文件解析xml时的注释。
I've tried clojure.xml/parse
as well as a couple other really nice libs from github; they all work fine except that every one strips out the comments during parsing.
我已经尝试过clojure.xml / parse以及来自github的其他几个非常好的库;他们都工作正常,除了每个人在解析过程中删除注释。
Can anyone recommend a lib or perhaps a technique with clojure.xml
that I'm not aware of?
任何人都可以推荐一个lib或者一个我不知道的clojure.xml技术吗?
1 个解决方案
#1
0
I ended up doing some preparsing on the xml string. Seems a bit hacky but it'll do for the moment.
我最终在xml字符串上做了一些预处理。看起来有点hacky但它现在会做。
(defn preparse-comments
[s]
(-> s
(s/replace #"<!--" "<comment>")
(s/replace #"-->" "</comment>")))
(defn revert-comments
[s]
(-> s
(s/replace #"<comment>" "<!--")
(s/replace #"</comment>" "-->")))
(defn load-data
[xml-filename]
(-> xml-filename
(slurp)
(preparse-comments)
(clojure.data.xml/parse-str)))
(defn emit-and-prep
[data]
(-> data
(clojure.data.xml/emit-str)
(revert-comments)))
Edit: the above is a pretty barebones approach; if you have tag-bracketing artifacts (e.g., />
and so on) in your comments, you'd have to appropriately account for them too somehow so that the preparsing does not render your xml invalid.
编辑:以上是一个非常准确的方法;如果你的评论中有标记包围工件(例如/>等等),你必须以某种方式对它们进行适当的考虑,以便预先制作不会使你的xml无效。
#1
0
I ended up doing some preparsing on the xml string. Seems a bit hacky but it'll do for the moment.
我最终在xml字符串上做了一些预处理。看起来有点hacky但它现在会做。
(defn preparse-comments
[s]
(-> s
(s/replace #"<!--" "<comment>")
(s/replace #"-->" "</comment>")))
(defn revert-comments
[s]
(-> s
(s/replace #"<comment>" "<!--")
(s/replace #"</comment>" "-->")))
(defn load-data
[xml-filename]
(-> xml-filename
(slurp)
(preparse-comments)
(clojure.data.xml/parse-str)))
(defn emit-and-prep
[data]
(-> data
(clojure.data.xml/emit-str)
(revert-comments)))
Edit: the above is a pretty barebones approach; if you have tag-bracketing artifacts (e.g., />
and so on) in your comments, you'd have to appropriately account for them too somehow so that the preparsing does not render your xml invalid.
编辑:以上是一个非常准确的方法;如果你的评论中有标记包围工件(例如/>等等),你必须以某种方式对它们进行适当的考虑,以便预先制作不会使你的xml无效。