xmlutil icon indicating copy to clipboard operation
xmlutil copied to clipboard

inline tag with one attribute

Open SchweinchenFuntik opened this issue 3 years ago • 1 comments

I have this xml structure:

...
<Header>
  <ModelInfo value="WorkspaceSettings" />
  <ModelVersion value="1.3.0.0" />
  <PersistencyVersion value="1.0.0.0" />
</Header>
...

As a result, you need to create as many as three classes:

@Serializable
class Header(
    val modelInfo: ModelInfo,
    val modelVersion: ModelVersion,
    val persistencyVersion: PersistencyVersion,
)

@Serializable class ModelInfo(val value: String)
@Serializable class ModelVersion(val value: String)
@Serializable class PersistencyVersion(val value: String)

I would like to simplify to:

annotation class XmlValueFromTagAttribute(val tag: String, val attribute: String = "value")

@Serializable
class Header(
    @XmlValueFromTagAttribute("ModelInfo", "value") val modelInfo: String,
    @XmlValueFromTagAttribute("ModelVersion", "value") val modelVersion: String,
    @XmlValueFromTagAttribute("PersistencyVersion", "value") val persistencyVersion: String,
)

this is an example of such a place, actually weight xml-file has this structure

PS: @XmlValueFromTagAttribute - long name, i would like it to be shorter

SchweinchenFuntik avatar Jan 21 '22 11:01 SchweinchenFuntik

Interesting one. Clearly not in the "philosophy" of XML. The use of "value" like attributes isn't really intended, and just using element content itself is possible. Another one is that tag names are sort-of expected to relate to type names. Note that you could use a single wrapper object as follows (I'll add it as a feature request anyway, but I'm not sure how best to go about achieving it):

@Serializable
data class StringValue(@XmlElement(false)val value: String)

@Serializable
class Header(
    @XmlSerialName("ModelInfo", "", "") val modelInfo: StringValue,
    @XmlSerialName("ModelVersion", "", "") val modelVersion: String,
    @XmlSerialName("PercistencyVersion", "", "")  val persistencyVersion: String,
)

Instead of StringValue you could also use a parameterised container.

pdvrieze avatar Jan 26 '22 09:01 pdvrieze