JAXB unmarshal Collection(套装)

时间:2017-09-13 21:34:13

标签: collections jaxb set marshalling unmarshalling

我正在测试编组和解组以下Java对象:

框架类:

@XmlRootElement (name = "framework")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class Framework implements Comparable<Framework>, Serializable {
    private static final long serialVersionUID = 1L;
    @XmlTransient
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String frameworkName;
    private String frameworkVersion;
    private String frameworkDescription;
    @XmlElementWrapper(name = "framework-domains")
    private Set<FrameworkDomain> frameworkDomainList = new HashSet<>();
    @XmlElementWrapper(name = "framework-comments")
    private Set<Comment> comments = new HashSet<>();

(仅包含getter,setter和nuber函数,无需任何其他注释)

域类:

@XmlRootElement(name = "domain")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class FrameworkDomain implements Comparable<FrameworkDomain>, Serializable {

    private static final long serialVersionUID = 1L;
    @XmlTransient
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String domainName;
    private String domainDescription;
    @XmlElementWrapper(name = "domain-requirements")
    private Set<Requirement> domainRequirements = new HashSet<>();
    @XmlElementWrapper(name = "domain-comments")
    private Set<Comment> domainComments = new HashSet<>();

要求等级:

@XmlRootElement(name = "requirement")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class Requirement implements Comparable<Requirement>, Serializable {

    private static final long serialVersionUID = 1L;
    @XmlTransient
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String requirementName;
    private String requirementDescription;
    private String requirementGuidance;
    @XmlElementWrapper(name = "sub-requirements")
    private Set<Requirement> requirementSubrequirementList = new HashSet<>();
    @XmlElementWrapper(name = "testing-procedures")
    private Set<TestingProcedure> requirementTestingProceduresList = new HashSet<>();
    @XmlElementWrapper(name = "requirement-comments")
    private Set<Comment> comments = new HashSet<>();

编组和解编码:

public static String exportFramework(Framework f) {
    java.io.StringWriter s = new java.io.StringWriter();
    try {
        JAXBContext jc = JAXBContext.newInstance(Framework.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        marshaller.marshal(f, s);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return s.toString();
}

public static Framework importFramework(java.io.InputStream xml) {
    intelicompliance.model.Framework f = null;

    try {
        JAXBContext jc = JAXBContext.newInstance(Framework.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        f = (intelicompliance.model.Framework) unmarshaller.unmarshal(xml);
    } catch (JAXBException ex) {
        ex.printStackTrace();
    }

    return f;
}

当我封送我创建的对象时,它会生成以下XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<framework>
    <frameworkName>PCI DSS</frameworkName>
    <frameworkVersion>3.3</frameworkVersion>
    <frameworkDescription>The Payment Card Industry Data Security Standard (PCI DSS) is a proprietary information security standard for organizations that handle branded credit cards from the major card schemes including Visa, MasterCard, American Express, Discover, and JCB.</frameworkDescription>
    <framework-domains>
        <frameworkDomainList>
            <domainName>Domain 1a</domainName>
            <domainDescription>Build and Maintain a Secure Network and Systems</domainDescription>
            <domain-requirements></domain-requirements>
            <domain-comments></domain-comments>
        </frameworkDomainList>
        <frameworkDomainList>
            <domainName>Domain 2a</domainName>
            <domainDescription>Protect Cardholder Data</domainDescription>
            <domain-requirements/>
            <domain-comments/>
        </frameworkDomainList>
        <frameworkDomainList>
            <domainName>Domain 3a</domainName>
            <domainDescription>Maintain a Vulnerability Management Program</domainDescription>
            <domain-requirements/>
            <domain-comments/>
        </frameworkDomainList>
        <frameworkDomainList>
            <domainName>Domain 4a</domainName>
            <domainDescription>Implement Strong Access Control Measures</domainDescription>
            <domain-requirements/>
            <domain-comments/>
        </frameworkDomainList>
        <frameworkDomainList>
            <domainName>Domain 5a</domainName>
            <domainDescription>Regularly Monitor and Test Networks</domainDescription>
            <domain-requirements/>
            <domain-comments/>
        </frameworkDomainList>
        <frameworkDomainList>
            <domainName>Domain 6a</domainName>
            <domainDescription>Maintain an Information Security Policy</domainDescription>
            <domain-requirements/>
            <domain-comments/>
        </frameworkDomainList>
    </framework-domains>
    <framework-comments/>
</framework>

......这正是我所期待的。

但是,当我尝试将XML转换回对象时,Set中只包含一个域 - 即导入(解组)进程在第一个之后忽略XML节点。

有谁知道为什么?或者,我做错了什么?

更新 我通过改变取得了一些进展:

private Set<FrameworkDomain> frameworkDomainList = new HashSet<>();

为:

private List<FrameworkDomain> frameworkDomainList = new LinkedList<>();

现在按预期导入所有子元素。但是,我更喜欢使用Set。

JAXB是否将List与(s)区别开来?

1 个答案:

答案 0 :(得分:0)

根据您对我的问题的回答:

我的意思是你确保每个不相同的对象都会返回不同的哈希值。

你的问题来自这样一个事实:你解组的每个对象都有相同的hashCode,因此你的集合认为每个元素都是相等的,只保留一个(最后一个JAXB进程,顺便说一下是第一个)

您应该能够通过向HashSet添加没有id的对象来确认此行为,只应保留最后一个。

它还解释了当您切换到链接列表时填充列表的原因。

使用不同的hashCode方法或编组ID来解决您的问题。

请记住,您应该始终在hashCode()方法旁边覆盖equals()方法,并确保a.equals(b)和a.hashCode == b.hashCode始终产生相同的结果。