Replies: 5 comments
-
The way the system works is that namespaces are handled automatically (although not necessarily optimally - it doesn't look at what other namespaces may be used by child tags - but will use prefixes found in the document). What you can do is have a custom serializer for the outer tag and in that serializer (if you find that the encoder implements |
Beta Was this translation helpful? Give feedback.
-
Can you make an example of both scenarios? I understand the idea, but having some issues putting it in place. |
Beta Was this translation helpful? Give feedback.
-
I've just pushed an example to dev to show how it works (note that it uses the |
Beta Was this translation helpful? Give feedback.
-
To be honest it's quite dirty solution imo. It would be great if we could somehow provide multiple namespaces through an annotation, without a need to implement whole serializer 🤔 At this moment I'm playing around something like this, as I can't achieve it though annotations: @Serializable
@SerialName("metadata")
internal data class Metadata(
@XmlElement(false)
val xmlns: String = "http://maven.apache.org/METADATA/1.1.0",
@XmlElement(false)
@SerialName("xmlns:xsi")
val xmlnsXsi: String = "http://www.w3.org/2001/XMLSchema-instance",
@XmlElement(false)
@SerialName("xsi:schemaLocation")
val xsiSchemaLocation: String = "http://maven.apache.org/METADATA/1.1.0 http://maven.apache.org/xsd/metadata-1.1.0.xsd",
) The output is correct: <?xml version='1.0' encoding='UTF-8'?>
<metadata xmlns="http://maven.apache.org/METADATA/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/METADATA/1.1.0 http://maven.apache.org/xsd/metadata-1.1.0.xsd">
</metadata> But unfortunately I'm not able to load this:
I'm also not able to use it as a final fields outside of the constructor. Do you think I can somehow do something like this, even a little dirty, without implementing custom serializer? |
Beta Was this translation helpful? Give feedback.
-
The example would be better when using the following: @Serializable
@XmlSerialName("metadata", "http://maven.apache.org/METADATA/1.1.0", "")
internal data class Metadata(
@XmlElement(false)
@XmlSerialName("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "xsi")
val xsiSchemaLocation: String = "http://maven.apache.org/METADATA/1.1.0 http://maven.apache.org/xsd/metadata-1.1.0.xsd",
) You shouldn't specify namespaces (or prefixes) using |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My goal is to build something similar to this:
But with
XmlSerialName
annotation I can only have 1 namespace. How can I add more than one? I don't see an option to add custom attributes to an element, that would solve the issue too.Beta Was this translation helpful? Give feedback.
All reactions