Java使用CDATA封送内部对象

时间:2019-11-27 14:44:54

标签: java xml jaxb marshalling cdata

我需要编组到xml的两个类。一个类(MessagingEntity类)是外部类(事件类)的内部类。我试图将两个对象编组在一起,以便将EventMessaging xml的结构包装为[[CDATA [/ * ... xml ... * /]]

事件类:

import com.sun.xml.internal.txw2.annotation.XmlCDATA;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "Event")
@XmlAccessorType(XmlAccessType.FIELD)
public class Event {

    @XmlAttribute
    private String id;

    @XmlAttribute
    private String topic;

    @XmlAttribute
    private String type;

    @XmlAttribute
    private String version;

    @XmlAttribute
    private String reference;

    @XmlAttribute
    private String system;

    @XmlAttribute
    private String timestamp;

    @XmlJavaTypeAdapter(XmlCDataAdapter.class)
    private MembershipMessageEntity messageEntity;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getSystem() {
        return system;
    }

    public void setSystem(String system) {
        this.system = system;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    @XmlCData
    public MembershipMessageEntity getMessageEntity() {
        return messageEntity;
    }

    public void setMessageEntity(MembershipMessageEntity messageEntity) {
        this.messageEntity = messageEntity;
    }

}

MessagingEntity类:

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement
public class MembershipMessageEntity {

    private String accountHolderBP;

    private String notificationTypeCode;

    private String preferredName;

    public String getAccountHolderBP() {
        return accountHolderBP;
    }

    public void setAccountHolderBP(String accountHolderBP) {
        this.accountHolderBP = accountHolderBP;
    }

    public String getPreferredName() {
        return preferredName;
    }

    public void setPreferredName(String preferredName) {
        this.preferredName = preferredName;
    }

    @XmlTransient
    public String getNotificationTypeCode() {
        return notificationTypeCode;
    }

    public void setNotificationTypeCode(String notificationTypeCode) {
        this.notificationTypeCode = notificationTypeCode;
    }
}

期望的XML:

    <?xml version="1.0" encoding="UTF-8"?>
<Event 
    timestamp="2019-08-05T09:12:02Z" 
    system="1.3.6.1.4.1.50603.3.2.1" 
    reference="3000612448" 
    version="1" 
    type="1.3.6.1.4.1.50603.2.3.14.6.4" 
    topic="1.3.6.1.4.1.50603.2.3.14.6" 
    id="005056B5-2A40-1EE9-ADEC-22BFB693B603">
    <![CDATA[
        <eventMessagingEntity> 
            <messageEntity> 
                <accountHolderBP>3000013482</accountHolderBP> 
                <preferredName>ZandileO</preferredName> 
            </messageEntity> 
        </eventMessagingEntity>
    ]]>
</Event>

请帮助。谢谢。

0 个答案:

没有答案
相关问题