Strange behavior when using XML attributes
See original GitHub issueMy jackson version 2.8.1:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.1</version>
</dependency>
I want to parse XML like this:
<out>
<in>
<first>fff</first>
<second>sss</second>
</in>
<in>
<first>fff2</first>
<second>sss2</second>
</in>
</out>
So my Out class:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.List;
@Getter
@Setter
@ToString
@JacksonXmlRootElement(localName = "out")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Out {
@JacksonXmlProperty(localName = "in")
@JacksonXmlElementWrapper(useWrapping = false)
private List<In> ins;
}
In class:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class In {
@JacksonXmlProperty(localName = "first")
private String first;
@JacksonXmlProperty(localName = "second")
private String second;
}
And App class:
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
public class App {
public static void main(String[] args) throws IOException {
XmlMapper xmlMapper = new XmlMapper();
Out out = xmlMapper.readValue(App.class.getResource("/test.xml"), Out.class);
System.out.println(out);
}
}
For XML above everything is good, I get Out(ins=[In(first=fff, second=sss), In(first=fff2, second=sss2)])
.
But I found few issues:
-
If I change
Out
element name to something else everything is working, is it OK?<bla-bla> <in> <first>fff</first> <second>sss</second> </in> <in> <first>fff2</first> <second>sss2</second> </in> </bla-bla>
I get
Out(ins=[In(first=fff, second=sss), In(first=fff2, second=sss2)])
-
Lets add some attribute to first element:
<bla-bla> <in> <first lang="en">fff</first> <second>sss</second> </in> </bla-bla>
And this fail with
Can not construct instance of xmlparsing.In: no String-argument constructor/factory method to deserialize from String value ('sss')
. More stranger that if we swap elements then everything will be good:<bla-bla> <in> <second>sss</second> <first lang="en">fff</first> </in> </bla-bla>
I get
Out(ins=[In(first=fff, second=sss)])
-
But this swap can’t help me for a long, because:
<bla-bla> <in> <second>sss</second> <first lang="en">fff</first> </in> <in> <first>fff2</first> <second>sss2</second> </in> </bla-bla>
I get only second one :
Out(ins=[In(first=fff2, second=sss2)])
-
One more strange thing:
<bla-bla> <in> <first lang="en">fff</first> <second></second> </in> </bla-bla>
I get two elements:
Out(ins=[In(first=fff, second=null), In(first=null, second=null)])
. Withoutlang="en"
everything is good.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Hi @cowtowncoder ,
My jackson version 2.9.3:
Kotlin version :
<kotlin.version>1.2.10</kotlin.version>
Ratpack version :<ratpack.kotlin.version>1.1.2</ratpack.kotlin.version>
I too am facing an issue while deserialising an element with an attribute.
Example XML that needs to be parsed :
classes that I am using :
Now in the first element that has both attribute value and textual content. whilst parsing I get this exception:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of
com.order.soap.Something
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘3050’)which is really weird. Coz I have other elements with just attribute value and all of them work just fine. Only this element which has both attribute value and textual content doesn’t work.
OK changing fields into
In.class
to someXmlNode
like:And
XmlNode.class
:solving my problems. I’m not trying it now, because previously I have very similar issue with parsing XML (https://github.com/FasterXML/jackson-dataformat-xml/issues/191). And that time
XmlNode
wasn’t helpful. I think it will be something similar. But in any case thank you for answer I think we can close this issue.