Home > Back-end >  How to parse xml like below with jackson?
How to parse xml like below with jackson?

Time:08-01

The input xml:

<root>
  <row>
    <foo>foo</foo>
    <bar>bar</bar>
  </row>
  <row>
    <foo>foo2</foo>
    <bar>bar2</bar>
  </row>
  <row>
    <foo>foo3</foo>
    <bar>bar3</bar>
  </row>
</root>

If I parse it like this:

XmlMapper = new XmlMapper();
Map map = xmlMapper.readValue(decodedXml, Map.class);

The result map will only contain the last row.

How to make it parse rows into an ArrayList?

CodePudding user response:

I think you can change into

List list = xmlMapper.readValue(decodedXml, List.class)

objects in list is LinkedHashMap

CodePudding user response:

Create two classes:

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "root")
public class Root {

    @JacksonXmlProperty(localName = "row")
    @JacksonXmlElementWrapper(useWrapping = false)
    private Row[] row;

    public Row[] getRow() {
        return row;
    }

    public void setRow(Row[] row) {
        this.row = row;
    }

}
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class Row {

    @JacksonXmlProperty(localName = "foo")
    private String foo;
    @JacksonXmlProperty(localName = "bar")
    private String bar;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}

For test:


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import generated.Root; // your package

public class Demo {

    public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
    
        ObjectMapper mapper = new ObjectMapper();
        XmlMapper xmlMapper = new XmlMapper();

        String xml = "<root>\r\n"
                  "  <row>\r\n"
                  "    <foo>foo</foo>\r\n"
                  "    <bar>bar</bar>\r\n"
                  "  </row>\r\n"
                  "  <row>\r\n"
                  "    <foo>foo2</foo>\r\n"
                  "    <bar>bar2</bar>\r\n"
                  "  </row>\r\n"
                  "  <row>\r\n"
                  "    <foo>foo3</foo>\r\n"
                  "    <bar>bar3</bar>\r\n"
                  "  </row>\r\n"
                  "</root>";
        Root map = xmlMapper.readValue(xml, Root.class);
        
        System.out.println("value "  mapper.writeValueAsString(map));
        
        
    }

}

Resul this:

value {"row":[{"foo":"foo","bar":"bar"},{"foo":"foo2","bar":"bar2"},{"foo":"foo3","bar":"bar3"}]}
  • Related