Fork me on GitHub
#clojure-nl
<
2022-03-29
>
Thierry11:03:16

Can anyone tell me why I cannot use clojure.data.xml/parse to parse xml (works) and then convert it back to string with clojure.data.xml/emit-str? I get the following error: (let [input ( "myxmlfile.xml") parsed (clojure.data.xml/parse input) parsed-with-headers {:headers {"Soap" ""} :body parsed} back-to-string (clojure.data.xml/emit-str parsed-with-headers)] (clojure.pprint/pprint back-to-string)) ; Execution error (IllegalArgumentException) at clojure.data.xml/fn$G (xml.clj:73). ; No implementation of method: :gen-event of protocol: #'clojure.data.xml/EventGeneration found for class: clojure.lang.PersistentArrayMap

arnout12:03:06

The parsed-with-headers format is not as the emit-str expects. See the examples on https://github.com/clojure/data.xml/. > XML elements can be created using the typical defrecord constructor functions or the element function used below or just a plain map with :tag :attrs :content keys ...

arnout12:03:00

Or maybe use the sexp-as-element function, for hiccup style

Thierry12:03:59

to use the sexp-as-element I need to parse every tag to output it again am i right? I just want to add a tag with subtags to an existing xml without having to parse and rebuild the entire xml

Thierry12:03:54

Ill have a look at the link

Thierry12:03:52

thanks for the reply!

Thierry12:03:48

Hmm I can see that an xml can be 'roundtripped' by creating one and then reading it back. I cannot find how to write back a read xml file. I have tried this too: (let [input ( "my.xml") parsed (clojure.data.xml/parse input)] (with-open [out-file (.FileWriter. "doc/temp.xml")] (emit parsed out-file))) But that gives me the following error: Prefix cannot be null And I tried to solve that by adding the parsed-with-headers: (let [input ( "my.xml") parsed (clojure.data.xml/parse input) parsed-with-headers {:headers {"Soap" ""} :body parsed}] (with-open [out-file (.FileWriter. "doc/temp.xml")] (emit parsed-with-headers out-file))) That gives me this error: No implementation of method: :gen-event of protocol: #'clojure.data.xml/EventGeneration found for class: clojure.lang.PersistentArrayMap Which comes back to the initial question.

Thierry13:03:50

Hmm its not reading back the Elements correct

Thierry13:03:31

The parsed xml starts like this: #clojure.data.xml.Element {:attrs #:xsi{:schemaLocation " gds801_573.xsd"}, :content (#clojure.data.xml.Element {:attrs {}, :content (#clojure.data.xml.Element {:attrs {}, :content ("573"), :tag :Berichtcode} #clojure.data.xml.Element

Thierry13:03:00

and goes on with new elements etc

Thierry13:03:08

Think I have found why a parsed xml wont write back, it's the #:xsi in :attrs that causes this

Thierry13:03:51

the attributes containt a colon in the keyname xsi:schemaLocation etc

Thierry13:03:10

right, theres the culprit

Thierry13:03:22

namespace support is not in the version im using

borkdude13:03:08

use the latest alpha version

Thierry11:03:35

I am probably doing something wrong or forgetting a crucial part

Thierry11:03:54

my goal is to read in an xml file, append to it and emit it back to an xml file