jeudi 29 janvier 2015

Jackson-XML. How can I ignore an XML attribute?


Vote count:

0




I have the following XML file:



<Channel>
<name>test</name>
<number type="example">123</number>
</Channel>


and the following Java class:



public class Channel {
public String name;
public Integer number;

@Override
public String toString() {
return "Channel{" + "name='" + name + '\'' + ", number=" + number + '}';
}
}


I want to use Jackson to read the XML into an object of class Channel. Here's how I tried to do this:



JacksonXmlModule module = new JacksonXmlModule();
XmlMapper xmlMapper = new XmlMapper(module);
InputStream stream = App.class.getResourceAsStream(FILE_NAME);

Channel value = xmlMapper.readValue(stream, Channel.class);
System.out.println(value);


But I get the following error:



Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token
at [Source: java.io.BufferedInputStream@158ec7a7; line: 3, column: 5] (through reference chain: org.robinski.Channel["number"])


I expected to get the following output:



Channel{name='test', number=123}


I know that it's the XML attribute 'type="example"' that causes this problem. When I remove the attribute, everything works. But I cannot just manually remove it, because normally I receive the XML from external source.


You can see the whole source code here: http://ift.tt/15XG8Fs .


What can I do to parse the XML file into an object of class Channel using Jackson?



asked 1 min ago







Jackson-XML. How can I ignore an XML attribute?

Aucun commentaire:

Enregistrer un commentaire