与Jackson相同的嵌套XML标签

Mic*_*ens 5 java xml jackson

我正在用Jackson解析以下外部XML。

<SomeRootObject>
  <Events>
    <Event>
      <EventID>248739296</EventID>
      ...
      <Event>1709</Event>
      ...
Run Code Online (Sandbox Code Playgroud)

我为“事件”定义了一个POJO。

@JacksonXmlRootElement(localName = "Event")
public class MyEvent {
    @JsonProperty("EventID")
    public String eventID;

    ...

    @JsonProperty("Event")
    public int event;

    ...
Run Code Online (Sandbox Code Playgroud)

如您所见,此POJO中的一个字段也被映射为“事件”。因此,杰克逊抱怨说它无法从事件中创建一个int:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token
at [Source: java.io.StringReader@12417468; line: 1, column: 280] (through reference chain: be.parkd.api.tnt.ram.model.RamEvents[“Event”]->java.util.ArrayList[0]->be.parkd.api.tnt.ram.model.RamEvent[“Event”]).
Run Code Online (Sandbox Code Playgroud)

杰克逊可以处理此案吗?

我想到的一个肮脏的解决方法是对XML进行预处理以更改基础事件,但是我希望使用一种更干净的解决方案。

jsc*_*sse 4

变体1

<Event>以下示例读取包装在元素中的元素列表<Events>。它<Event>本身包含另一个嵌套<Event>元素。这对于杰克逊来说似乎不是问题。

注意:我用作TypeReference<List<Event>>() {}序列化规则。

@Test
public void test1() throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new XmlMapper();
    List<Event> event=mapper.readValue("<Events><Event><EventID>248739296</EventID><Event>1709</Event></Event><Event><EventID>248739297</EventID><Event>1710</Event></Event></Events>", new TypeReference<List<Event>>() {
    });
    System.out.println(toString(event));
}

public String toString(Object obj) {
    try {
        StringWriter w = new StringWriter();
        new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, obj);
        return w.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用Event.java

@JacksonXmlRootElement(localName = "Event")
public class Event {
    @JsonProperty("EventID")
    private String eventID;
    @JsonProperty("Event")
    private int event;
    public String getEventID() {
        return eventID;
    }
    public void setEventID(String eventID) {
        this.eventID = eventID;
    }
    public int getEvent() {
        return event;
    }
    public void setEvent(int event) {
        this.event = event;
    }
}
Run Code Online (Sandbox Code Playgroud)

印刷

[ {
  "EventID" : "248739296",
  "Event" : 1709
}, {
  "EventID" : "248739297",
  "Event" : 1710
 } ]
Run Code Online (Sandbox Code Playgroud)

所以,它有效!

变体2

@Test
public void test2() throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new XmlMapper();
    SomeRootObject object=mapper.readValue("<SomeRootObject><Events><Event><EventID>248739296</EventID><Event>1709</Event></Event><Event><EventID>248739297</EventID><Event>1710</Event></Event></Events></SomeRootObject>", SomeRootObject.class);
    System.out.println(toString(object));
}
Run Code Online (Sandbox Code Playgroud)

使用 SomeRootObject.class

@JacksonXmlRootElement(localName = "SomeRootObject")
public class SomeRootObject {
    @JsonProperty("Events")
    List<Event> events;
    public SomeRootObject() {

    }
    public List<Event> getEvents() {
        return events;
    }
    public void setEvents(List<Event> events) {
        this.events = events;
    }
}
Run Code Online (Sandbox Code Playgroud)

印刷

{
  "Events" : [ {
    "EventID" : "248739296",
    "Event" : 1709
  }, {
    "EventID" : "248739297",
    "Event" : 1710
  } ]
}
Run Code Online (Sandbox Code Playgroud)

也有效!