使用clojure.xml / parse和clojure.xml / emit在Clojure中使用roundtripping xml [英] Roundtripping xml in Clojure using clojure.xml/parse and clojure.xml/emit

查看:104
本文介绍了使用clojure.xml / parse和clojure.xml / emit在Clojure中使用roundtripping xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的往返产生invaild xml,因为结果没有正确转义,即属性值包含'代替'。我是做错了什么,还是这是一个错误?

Below roundtrip produces invaild xml as the result is not escaped correctly, i.e. the attribute values contain ' instead of apos;. Am I doing somthing wrong or is this a bug?

(ns xml-test
  (:require [clojure.xml :as xml])
  (:require [clojure.zip :as zip]))

(def test-xml "<?xml version="1.0" encoding="UTF-8"?> <main> <item attr='&apos;test&apos;'> </item> </main>")

(def s (ByteArrayInputStream. (.getBytes test-xml "UTF-8")))

(xml/emit (zip/root (zip/xml-zip (clojure.xml/parse s))))

output:

<?xml version='1.0' encoding='UTF-8'?>
<main>
<item attr=''test''/>
</main>
nil


推荐答案

快速和 clojure.xml / emit-element (它被 clojure.xml / emit 调用)将任何字符编码为XML实体;实际上,它让属性值直接通过。我想这意味着 clojure.xml 在其可用性相当有限;你应该使用 clojure.contrib.lazy-xml 。我没有在第一个关于XML发送的问题的答案中提到它,我没有意识到这种情况会发生。

I've checked the source quickly and clojure.xml/emit-element (which gets called by clojure.xml/emit) makes no effort whatever to encode any characters as XML entities; in fact, it lets attribute values straight through. I guess this means clojure.xml is quite limited in its usability; you should use clojure.contrib.lazy-xml instead. My apologies for not mentioning it in the answer to your first question on XML emitting, I didn't realise stuff like this would happen.

使用 clojure.contrib.lazy-xml ,您可以执行以下操作:

With clojure.contrib.lazy-xml, you can do the following:

user> (lazy-xml/emit
       (lazy-xml/parse-trim
        (java.io.StringReader. "<foo bar=\"&apos;&quot;&quot;&apos;\"/>")))
<?xml version="1.0" encoding="UTF-8"?><foo bar="'&quot;&quot;'"/>

如果你真的想使用 clojure.xml ,您必须传递 clojure.xml / emit 并使用您选择的XML生产者。实际上,你可以使用 clojure.xml / parse ,修改结果,然后将其传递给 clojure.contrib.lazy-xml / emit ; XML的Clojure表示的结构与两个库是相同的,但只有后者正确发出。

If you really wanted to use clojure.xml, you'd have to pass on clojure.xml/emit and use an XML producer of your choice instead. Well, actually, you can use clojure.xml/parse, mangle the result, then pass it to clojure.contrib.lazy-xml/emit; the structure of the Clojure representation of the XML is the same with both libraries, but only the latter does proper emitting.

这篇关于使用clojure.xml / parse和clojure.xml / emit在Clojure中使用roundtripping xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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