如何将(marshall)数组序列化为每个元素

How to serialize(marshall) array to each element

我正在尝试使用 Jackson 库编组和解组 XML 文件。

源文件是下面的'team.xml'文件。 '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>

”元素是两个,但没有像“”一样包装到容器元素中。

无论如何,我使用@JsonSetter 成功反序列化(解组)'team.xml' 到'Team.class' 到'Team.class' 的'public void addMember(Member)'。

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

然后我写一个Team.class对象到XML字符串。 结果如下.... :(


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>

两个“”元素被包装到“”父元素中。

如何将 Team.class 的对象序列化(编组)为 XML 字符串等于 'input XML string'?

即,如何将数组值序列化(编组)为每个值?

请告诉我如何解决这个问题...

谢谢。

------------ JAVA 类 ----------

Member.class

package Whosebug.question.xml;

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

/**
 * 
 * @since 2021. 12. 21.
 * @version
 * @author Park_Jun_Hong_(parkjunhong77@gmail.com)
 */
@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 Whosebug.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_(parkjunhong77@gmail.com)
 */
@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();
    }
}

很遗憾,您正试图以错误的方式反序列化和序列化 xml。 Jackson 库包含 xml 的特定注释,例如 JacksonXmlProperty and for the xml lists there is the specific JacksonXmlElementWrapper 注释,因此您必须像下面这样重写 类:

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

}

有关任何更多信息,您可以参考我包含的链接,下面是您包含的 xml 的反序列化和序列化示例:

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