使用Jackson将带有重复嵌套标签的XML反序列化

时间:2019-09-29 21:41:02

标签: java xml jackson xml-serialization

我正在尝试反序列化具有相同名称的嵌套属性的xml,但是包装器名称对于每个属性都是唯一的。下面的示例XML。

我尝试过使用包装器和属性名称的切换,但似乎不起作用。

let password = '123';
let myHashedPassword = hashPassword(password);

我正在尝试将上述XML反序列化为function hashPassword(password) { bcrypt.genSalt(10, function(error, salt) { bcrypt.hash(password, salt, function(error, hash) { // In most cases at this point hash is saved to the database. // However is there a pattern to return its value to the outer function and then app.js? // With this being async is that even possible? }); }); } <response> <string> <item>Sample string.</item> <item>Another sample string.</item> </string> <number> <item>123123123</item> <item>900912</item> </number> </response> 变量。

2 个答案:

答案 0 :(得分:2)

我设法使其创建一个ArrayList的对或包装器作为内部类:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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="response")
public class ResponseObjectList implements Serializable {

    @JacksonXmlProperty(localName = "string")
    private StringArrayListContainer string;

    @JacksonXmlProperty(localName = "number")
    private IntegerArrayListContainer number;

    public ResponseObjectList() {
        super();
    }

    public ResponseObjectList(List<String> stringItems, List<Integer> intItems) {
        super();
        this.string = new StringArrayListContainer(stringItems);
        this.number = new IntegerArrayListContainer(intItems);
    }

    public StringArrayListContainer getString() {
        return string;
    }

    public void setString(StringArrayListContainer string) {
        this.string = string;
    }

    public IntegerArrayListContainer getNumber() {
        return number;
    }

    public void setNumber(IntegerArrayListContainer number) {
        this.number = number;
    }

    public static class StringArrayListContainer extends ArrayListContainer<String>{

        public StringArrayListContainer() {
            super();
        }

        public StringArrayListContainer(List<String> item) {
            super(item);
        }

    }

    public static class IntegerArrayListContainer extends ArrayListContainer<Integer>{

        public IntegerArrayListContainer() {
            super();
        }

        public IntegerArrayListContainer(List<Integer> item) {
            super(item);
        }

    }

    public static class ArrayListContainer<T extends Serializable>{ 

        @JacksonXmlElementWrapper(useWrapping=false)
        @JacksonXmlProperty(localName="item")
        private List<T> item;

        public ArrayListContainer(List<T> item) {
            super();
            this.item = item;
        }

        public ArrayListContainer() {
            super();
        }

        public List<T> getItem() {
            return item;
        }

        public void setItem(List<T> item) {
            this.item = item;
        }

    }

}

测试看起来不错:

@Test
    public void test3() throws JsonProcessingException {
        ResponseObjectList response = new ResponseObjectList(
                    Arrays.asList(new String[] {"Sample string.","Another sample string"}),
                    Arrays.asList(new Integer[] {123123123,900912})
                );
        XmlMapper xmlMapper = new XmlMapper();
        String content = xmlMapper.writeValueAsString(response);
        this.logger.debug("content: " + content);
        // content: <response xmlns=""><string><item>Sample string.</item><item>Another sample string</item></string><number><item>123123123</item><item>900912</item></number></response>
    }

    @Test
    public void test4() throws JsonParseException, JsonMappingException, IOException {
        String xml = 
                "<response>"
                + "<string>"
                + "<item>Sample string.</item>"
                + "<item>Another sample string</item>"
                + "</string>"
                + "<number>"
                + "<item>123123123</item>"
                + "<item>900912</item>"
                + "</number>"
                + "</response>";

        XmlMapper xmlMapper = new XmlMapper();
        ResponseObjectList object = xmlMapper.readValue(xml, ResponseObjectList.class);
        Assert.assertFalse(object.getString().getItem().isEmpty());
        Assert.assertFalse(object.getNumber().getItem().isEmpty());
    }

在包装程序和测试中,我都使用List而不是ArrayList

答案 1 :(得分:1)

对于版本<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>A simple HTML Page</title> <script></script></head> <body> <h1>Simple HTML</h1> <p>Well this is nice!</p> </body> </html> 的简单2.9.9带有POJO注释,按预期工作:

JacksonXmlElementWrapper