如何使用Jackson处理命名空间的重复xml标记

时间:2022-09-19 18:04:23

Is it possible using Jackson, to handle duplicate xml tags based off of their namespace?

是否可以使用Jackson来处理基于其命名空间的重复xml标签?

The code below throws a JsonMappingException : Multiple fields representing property "url"

下面的代码抛出一个JsonMappingException:表示属性“url”的多个字段

private final ObjectMapper xmlMapper = new XmlMapper();

private static final String xml =
        "<example xmlns:test='http://test.com/'>" +
            "<test:url>www.namespace.com'</test:url>" +
            "<url>www.url.com'</url>" +
        "</example>";

@Test
public void parseXml() throws Exception {
    Example example = xmlMapper.readValue(xml, Example.class);

    assert example.namespaceUrl.equals("www.namespace.com");
}

public static class Example {

    @JsonProperty("url")
    public String namespaceUrl;

    @JsonProperty("url")
    public String url;

}

Thanks a bunch!

谢谢你!

1 个解决方案

#1


2  

There is no way to do that currently -- you can not use properties that only differ by namespace. This is not a fundamental limitation; that is, with more work, it could be supported; but it is current limitation.

目前无法做到这一点 - 您不能使用仅按名称空间不同的属性。这不是一个基本的限制;也就是说,有了更多的工作,就可以得到支持;但这是目前的限制。

#1


2  

There is no way to do that currently -- you can not use properties that only differ by namespace. This is not a fundamental limitation; that is, with more work, it could be supported; but it is current limitation.

目前无法做到这一点 - 您不能使用仅按名称空间不同的属性。这不是一个基本的限制;也就是说,有了更多的工作,就可以得到支持;但这是目前的限制。