【问题标题】:How to convert POJO to XML with Jackson如何使用 Jackson 将 POJO 转换为 XML
【发布时间】:2014-01-31 22:17:45
【问题描述】:

我正在寻找如何将 POJO 或 JSON 转换为 XML 的最佳解决方案,并将所有属性放在正确的位置。目前,杰克逊看起来是最方便的方式。我能够将 POJO 序列化为没有属性的 XML。

POJO 测试用户

public class TestUser extends JsonType
{
    @JsonProperty("username")
    private final String username;
    @JsonProperty("fullname")
    private final String fullname;
    @JsonProperty("email")
    private final String email;
    @JsonProperty("enabled")
    private final Boolean enabled;

    @JsonCreator
    public TestUser(
        @JsonProperty("username") String username, 
        @JsonProperty("fullname") String fullname, 
        @JsonProperty("email") String email,
        @JsonProperty("enabled") Boolean enabled)
        {
            this.username = username;
            this.fullname = fullname;
            this.email = email;
            this.enabled = enabled;
        }
        @JsonGetter("username")
        public String getUsername()
        {
            return username;
        }
        @JsonGetter("fullname")
        public String getFullname()
        {
            return fullname;
        }
        @JsonGetter("email")
        public String getEmail()
        {
            return email;
        }
        @JsonGetter("enabled")
        public Boolean getEnabled()
        {
            return enabled;
        }
    }
}

代码如下:

public void testJsonToXML() throws JsonParseException, JsonMappingException, IOException
{
    String jsonInput = "{\"username\":\"FOO\",\"fullname\":\"FOO BAR\", \"email\":\"foobar@foobar.com\", \"enabled\":\"true\"}";

    ObjectMapper jsonMapper = new ObjectMapper();
    TestUser foo = jsonMapper.readValue(jsonInput, TestUser.class);
    XmlMapper xmlMapper = new XmlMapper();
    System.out.println(xmlMapper.writer().with(SerializationFeature.WRAP_ROOT_VALUE).withRootName("product").writeValueAsString(foo));
}

现在它返回这个

<TestUser xmlns="">
    <product>
        <username>FOO</username>
        <fullname>FOO BAR</fullname>
        <email>foobar@foobar.com</email>
        <enabled>true</enabled>
    </product>
</TestUser>

这很好,但我需要变量 enabled 成为 username 的属性,然后我需要将 xmlns 和 xsi 属性添加到根元素,以便 XML 结果看起来像这样

<TestUser xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="testUser.xsd">
    <product>
        <username enabled="true">FOO</username>
        <fullname>FOO BAR</fullname>
        <email>foobar@foobar.com</email>
    </product>
</TestUser> 

我发现了一些使用@JacksonXmlProperty 的示例,但它只是将属性添加到根元素。

感谢您的帮助

【问题讨论】:

  • 您在TestUser 中扩展的JsonType 的完整包名称是什么?

标签: java xml json jackson pojo


【解决方案1】:

有趣的问题:注入额外的数据。目前没有这样做的功能;但我认为可以在@JsonRootName (schema=URL?) 中添加一个新属性,这将允许添加一个或多个模式映射?

我继续提交:

https://github.com/FasterXML/jackson-dataformat-xml/issues/90

添加一些应该起作用的东西;随时添加 cmets、建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-24
    • 2020-09-07
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    相关资源
    最近更新 更多