7

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"

Fred
  • 741
  • 2
  • 10
  • 22
  • Try turning your fields into properties and making them public. – ylax Aug 08 '18 at 20:03
  • Hmm what do you mean exactly? I have to add that unfortunately the XML cannot be modified as it is given by an external party. – Fred Aug 08 '18 at 20:06

4 Answers4

10

Try the following:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Bundle {
   ...
}

Alternatively:

mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Thanks what you suggested helps as it allows to ignore the tag like I wanted. Do you also have any idea how I can bind my Java object property `id` to the XML value `myBundleId` without having to create a subclass named `Id` ? – Fred Aug 08 '18 at 20:32
  • @Fred You could try `@JsonProperty`. But it goes beyond the scope of your original question. Please, ask a new question and add all relevant details to it. – cassiomolin Aug 09 '18 at 08:10
  • @Fred If my answer solves your current issue, don't forget to update an accept it :) – cassiomolin Aug 09 '18 at 08:10
0

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);

Rich Ackroyd
  • 117
  • 6
0

to bind properties you can use @JsonProperty Jackson Annotations

jker
  • 465
  • 3
  • 13
0

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>
jurgen
  • 325
  • 1
  • 11