Wie konvertieren von POJO in XML mit Jackson

Ich bin auf der Suche für die besten Lösung, wie Sie zu konvertieren POJO-oder JSON zu XML mit allen atttributes in der richtigen Stelle. Für jetzt, Jackson sieht aus wie der bequemste Weg. Ich bin in der Lage zu serialisieren POJO in XML ohne Attribute.

POJO TestUser

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;
        }
    }
}

Hier ist der code:

public void testJsonToXML() throws JsonParseException, JsonMappingException, IOException
{
    String jsonInput = "{\"username\":\"FOO\",\"fullname\":\"FOO BAR\", \"email\":\"[email protected]\", \"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));
}

Und jetzt gibt es diese

<TestUser xmlns="">
    <product>
        <username>FOO</username>
        <fullname>FOO BAR</fullname>
        <email>[email protected]</email>
        <enabled>true</enabled>
    </product>
</TestUser>

Ist nett, aber ich brauche die variable enabled sein Attribut username und dann muss ich hinzufügen, xmlns und xsi Attribute des root-Elements, also das XML-Ergebnis sieht wie folgt aus

<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>[email protected]</email>
    </product>
</TestUser> 

Fand ich einige Beispiele für die Verwendung von @JacksonXmlProperty aber er fügt lediglich das Attribut auf das root-element.

Danke für die Hilfe

  • Was ist die komplette package-Namen für JsonType dem Sie sich in Ihrem TestUser?
InformationsquelleAutor Jamalissimo | 2014-01-13
Schreibe einen Kommentar