Reputation: 781
I want to know if it's possible to ignore one or many nodes when parsing XML using Jackson ML module.
I want to be able to parse this XML
<bundle>
<id value="myBundleId"/>
<meta>
<profile value="http://myurl/profile1" />
<profile value="http://myurl/profile2" />
<tag>
<system value="https://myurl/system" />
<code value="myAppCode"/>
</tag>
</meta>
<type value="message" />
</bundle>
into this POJO object
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Data;
@Data
public class Bundle {
@JacksonXmlElementWrapper(localName = "id")
@JacksonXmlProperty(isAttribute = true, localName = "value")
private String id;
@JacksonXmlElementWrapper(localName = "type")
@JacksonXmlProperty(isAttribute = true, localName = "value")
private String type;
}
Right now it's not working as I think the annotation @JacksonXmlElementWrapper is only working with lists.
It also gives me the following error message :
java.lang.IllegalArgumentException: Conflicting setter definitions for property "value"
Upvotes: 9
Views: 15655
Reputation: 325
If you don't mind using a different library, SimpleXml does this by default:
public class Bundle {
@XmlWrapperTag("id")
@XmlName("value")
@XmlAttribute
private String id;
@XmlWrapperTag("type")
@XmlName("value")
@XmlAttribute
private String type;
}
And then serialize and print:
final SimpleXml simple = new SimpleXml();
final Bundle bundle = simple.fromXml(xml, Bundle.class);
System.out.println(bundle.id + " : " + bundle.type);
This will print:
myBundleId : message
SimpleXml is in maven central
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>1.5.5</version>
</dependency>
Upvotes: 0
Reputation: 131177
Try the following:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Bundle {
...
}
Alternatively:
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Upvotes: 11
Reputation: 127
If I recall right you can set this on the object mapper and it will avoid exceptions being thrown on unmatched nodes.
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Upvotes: 0