I'm trying to read in XML data from a service (and I cannot change the data) and having a problem with the Jackson XmlMapper
. If I have XML like so:
<entry>
<title type="text">W411638</title>
</entry>
It gives me back the following map:
title: ["": "W411638", "type": text]
I'm trying to turn this into an object using the following code:
XmlMapper xmlMapper = new XmlMapper()
Entry entry = xmlMapper.readValue(xmlData, Entry.class)
And my entry class looks like so:
class Entry {
static class Title {
//String __; //-- This is what I can't figure out --
String type;
}
Title title;
}
The problem is I can't find any way of getting that title text ("W411638") into the entry object. The type pulls in fine and I can get it by doing entry.title.type and it's correct, I just don't know how to get that title value.
This works for me as a standalone Groovy Script...
@Grab( 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.0.5' )
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText
class Entry {
static class Title {
public String type
@JacksonXmlText
public String value
public String toString() {
"$type -> $value"
}
}
public Title title
public String toString() {
"Entry [$title]"
}
}
def xml = '''<entry>
| <title type="text">W411638</title>
|</entry>'''.stripMargin()
def xmlMapper = new XmlMapper()
Entry pojo = xmlMapper.readValue( xml, Entry )
println pojo // prints 'Entry [text -> W411638]'
Fingers crossed it works for you too!