What's the "state-of-the-art" way to generate a si...
# getting-started
v
What's the "state-of-the-art" way to generate a simple XML file with Kotlin? Is it to use KxS with https://github.com/pdvrieze/xmlutil, or is there something else, for example like the
MarkupBuilder
in Groovy? Kotlin version is 1.7.10, as it is for a Gradle 7.6 supporting plugin.
h
I regularly use xmlutil but honestly it takes some time to understand its API.
v
Trying it right now, just have to find which version still supports 1.7.10. Latest is compiled with 1.9
Do you know a way to make a list serialized with tags in a parent tag without specifying the child tag names? I'd like to have the child tag name configured at the child type. But the only way I found to make them rendered in a parent tag is
XmlChildrenName
which forces to give the child name. Besides that it is a strange side-effect, that it causes a wrapping tag to be used.
h
Can you provide a sample of your xml you want to produce?
v
Copy code
<foo>
    <bar>
        <baz bam="bam1"/>
        <baz bam="bam2"/>
    </bar
</foo>
Foo
has a list of
Baz
. I specify
@SerialName("baz")
on
Baz
. But I do not see how to make it wrapped in
bar
without using
@XmlChildrenName
where I have to specify
baz
again.
h
Hm, I just used the lazy workaround and created another class Bar containing a list of
Baz
.
v
Hm, yeah, that's one possibility, thanks, but such an unnecessary indirection 😄
And how do I set an xml ns on the root node without any prefix? Using the default value for the prefix results in an empty
xmlns
attribute and a prefix for the intended namespace. 😕
Ah, just not using
XmlSerialName
but having a property called
xmlns
hackyyy
Hm, no, that only works properly with
encodeToString
, but not with
encodeToWriter
. 😞
Ah, there it works with using
XmlSerialName
😒
h
Yes, you have to use XmlSerialName everywhere...
v
No, just on the root element seems to be enough
h
It depends on your xml, if you want to use different namespaces, I tried a mix of
SerialName
and
XmlSerialName
and choose the latter, otherwise the namespaces were wrong. Anyway, we use it only for automatic soap api generating.
👌 1
124 Views