Home > OS >  How to serialize(marshall) array to each element
How to serialize(marshall) array to each element

Time:12-22

I' trying to marshall and unmarshall XML file using Jackson library.

The source file is a 'team.xml' file below. 'team.xml'

<team>
    <member>
        <name>Mike</name>
        <tel_no>123-1234-1234</tel_no>
    </member>
    <member>
        <name>John</name>
        <tel_no>123-4567-8901</tel_no>
    </member>
</team>

'<member>' element are two but are not wrapped into a container element like a '<members>'.

Anyway I succeeded to deserialize(unmarshall) 'team.xml' to 'Team.class' using @JsonSetter to 'public void addMember(Member)' of 'Team.class'.

String xmlStr = //
        "<team>\n"   //
                "    <member>\n"   //
                "        <name>Mike</name>\n"   //
                "        <tel_no>123-1234-1234</tel_no>\n"   //
                "    </member>\n"   //
                "    <member>\n"   //
                "        <name>John</name>\n"   //
                "        <tel_no>123-4567-8901</tel_no>\n"   //
                "    </member>\n"   //
                "</team>";

XmlMapper mapper = new XmlMapper();
Team team = mapper.readValue(xmlStr, Team.class);

System.out.println("------------ unmarshall -----------");
System.out.println(team);

Then I write an object of Team.class to XML string. The result is folloiwng.... :(


System.out.println("------------ marshall -----------");
System.out.println(mapper.writeValueAsString(team));

XML string ...
<team>
    <member>
        <member>
            <name>Mike</name>
            <tel_no>123-1234-1234</tel_no>
        </member>
        <member>
            <name>John</name>
            <tel_no>123-4567-8901</tel_no>
        </member>
    </member>
    <name/>
</team>

Two '<member> elements are wrapped into '<member>', parent element.

How to serialize(marshall) an object of Team.class to XML string equals to 'input XML string'?

That is, How to serialize(marshall) an array value to each value?

Please, tell me how to solve this problem...

Thanks.

---------- JAVA Classes -----------

Member.class

package stackoverflow.question.xml;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

/**
 * 
 * @since 2021. 12. 21.
 * @version
 * @author Park_Jun_Hong_([email protected])
 */
@JsonRootName("member")
public class Member {

    @JsonProperty("name")
    private String name;
    @JsonProperty("tel_no")
    private String telNo;

    public Member() {
    }

    public String getName() {
        return name;
    }

    public String getTelNo() {
        return telNo;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setTelNo(String telNo) {
        this.telNo = telNo;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Member [name=");
        builder.append(name);
        builder.append(", telNo=");
        builder.append(telNo);
        builder.append("]");
        return builder.toString();
    }
}

Team.class

package stackoverflow.question.xml;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonSetter;

/**
 * 
 * @since 2021. 12. 21.
 * @version
 * @author Park_Jun_Hong_([email protected])
 */
@JsonRootName("team")
public class Team {

    @JsonProperty("name")
    private String name;

    private List<Member> member;

    public Team() {
    }

    @JsonSetter("member")
    public void addMember(Member m) {
        if (m == null) {
            return;
        }

        if (this.member == null) {
            this.member = new ArrayList<>();
        }

        this.member.add(m);
    }

    public List<Member> getMember() {
        return member;
    }

    public String getName() {
        return name;
    }

    public void setMember(List<Member> member) {
        this.member = member;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Team [name=");
        builder.append(name);
        builder.append(", member=");
        builder.append(member);
        builder.append("]");
        return builder.toString();
    }
}

CodePudding user response:

Unfortunately you are trying to deserialize and serialize the xml in the wrong way. The Jackson library includes specific annotations for the xml like JacksonXmlProperty and for the xml lists there is the specific JacksonXmlElementWrapper annotation, so you have to rewrite your classes like below:

@Data
public class Team {

    @JacksonXmlProperty(localName = "member")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Member> member;
}

@Data
public class Member {

    @JacksonXmlProperty(localName = "name")
    private String name;
    @JacksonXmlProperty(localName = "tel_no")
    private String telNo;

}

For any further information you can refer to the links I included, below an example of deserialization and serialization of the xml you included:

Team team = mapper.readValue(xml, Team.class);
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String s = mapper.writeValueAsString(team);
System.out.println(s); //<-- it prints an xml as a string equal to the original one
  • Related