0

I have an xml file like this:

<TABLES>
<TABLE NAME="abcd" TIME="2013.05.27 00:00:00" >
<LINES>
<LINE TIME="2013.05.27 00:00:00" BEGINE="787465" END="787465"/>
</LINES>
<SPECIAL>
<DAY MASK="128" DATE="16714778" />
<WEEK NAME="abcde" PARAM="128" />
</SPECIAL> 
</TABLE>
</TABLES>

Problem is that, within the SPECIAL tag there are 2 types of elements DAY and WEEK. I am not able to parse it via jackson, is there any special way to handle it? I can parse the rest via POJO without any problem, but for special I need to set up 2 different classes

// DAY
class SPECIAL(
    @JacksonXmlProperty(localName = "MASK")
    val mask: String,

    @JacksonXmlProperty(localName = "DATE")
    val date: Int,
)

// WEEK
class SPECIAL(
    @JacksonXmlProperty(localName = "NAME")
    val name: String,

    @JacksonXmlProperty(localName = "PARAM")
    val param: Int,
)

1 Answer 1

1

Special has two different tags DAY and Week so you can segregate them into two POJO classes as

data class DAY(@JacksonXmlProperty(localName = "Date") val date: String, @JacksonXmlProperty(localName = "MASK")val mask: String)

data class WEEK(@JacksonXmlProperty(localName = "PARAM") val param: String,@JacksonXmlProperty(localName = "Name") val name: String)

and use them in SPECIAL class as:

data class SPECIAL(@JacksonXmlProperty(localName = "WEEK") val week: WEEK, @JacksonXmlProperty(localName = "DAY") val day: DAY)



To make the SPECIAL field optional, mark that field as nullable in your TABLE class using ? as:

data class TABLE(
    @JacksonXmlProperty(localName = "SPECIAL") val special: SPECIAL?,
    @JacksonXmlProperty(localName = "NAME") val name: String,
    @JacksonXmlProperty(localName = "TIME") val time: String
)

and SPECIAL class as

data class SPECIAL(
    @JacksonXmlProperty(localName = "WEEK") var week: WEEK?,
    @JacksonXmlProperty(localName = "DAY") var day: DAY?
)

View Complete Source Code

Sign up to request clarification or add additional context in comments.

4 Comments

OK, I did like proposed week or day is not mandatory so I made them optional, but then I had error Unrecognized field, not marked as ignorable. And when I annotated classed with @JsonIgnoreProperties(ignoreUnknown = true) parsing is finished wothout error, there is correct number of SPECIAL objects but in all of them DAY and WEEK is always null
@maxxxo @JsonIgnoreProperties(ignoreUnknown = true) will avoid the serialization and deserialization so better use @JsonInclude(Include.NON_NULL) for optional values and also use var with nullable fields var day: DAY? = null . Though it's better to mention the requirement once in question or post another question
I tried the Include annotation and optionals, but did not helped, this is the error: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "NAME" (class .Special), not marked as ignorable (4 known properties: "DAY", "WEEK", "day", "week"]) at [Source: (StringReader); line: 26, column: 58] (through reference chain: >.Table["SPECDAYS"]->java.util.ArrayList[0]->*.SpecDay["NAME"]) Main problem is that in Special Jackson expects properties from Day/ Week not Day/Week. I tried also to set isAttribute = true but no change
When I copied parameters from Day and Week into Special it worked fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.