2017-12-13 136 views
1

この件について既にいくつの質問がありますか。それでも私が理解しているのは、一般的なエラーで、私のコードは正解だと私は理解しています。それは、同僚が私を渡した別のプロジェクトのものとまったく同じだからです。@XmlRootElement注釈が存在しないため、要素として[...]型をマーシャリングできません。

しかし..私は持っている、このクラスCollaborationInfoを考えると、このエラー

unable to marshal type "modules.CollaborationInfo" as an element because it is missing an @XmlRootElement annotation 

package modules; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElementRef; 


@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "CollaborationInfo", propOrder = { 
    "AgreementRef", 
    "ConversationId" 
}) 


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

    public static class AgreementRef { 
     @XmlAttribute 
     private String pmode; 

     public String getPmode() { 
      return pmode; 
     } 
     public void setPmode(String pmode) { 
      this.pmode = pmode; 
     } 
    } 

    @XmlElementRef(name = "AgreementRef") 
    protected AgreementRef AgreementRef = new AgreementRef(); 

    @XmlElement(name="ConversationId") 
    protected String ConversationId; 


    public String getPmode() { 
     return AgreementRef.getPmode(); 
    } 

    public void setPmode(String value) { 
     this.AgreementRef.setPmode(value); 
    } 


    public String getConversationId() { 
     return ConversationId; 
    } 
    public void setConversationId(String value) { 
     this.ConversationId = value; 
    } 


} 

そしてメインのように():

public static void main(String[] args) throws Exception{ 
    JAXBContext contextObj = JAXBContext.newInstance(CollaborationInfo.class); 
    Marshaller marshallerObj = contextObj.createMarshaller(); 
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

    CollaborationInfo CI = new CollaborationInfo(); 
    CI.setPmode("String1"); 
    CI.setConversationId("String2"); 

    marshallerObj.marshal(CI, new FileOutputStream("C:\\OUT.xml")); 
} 
私はとして持つことができるはずです

出力(OUT.xml):

<?xml version="1.0" encoding="UTF-8"?> 
    <CollaborationInfo> 
     <AgreementRef pmode="String1"/> 
     <ConversationId>String2</ConversationId> 
    </CollaborationInfo> 

しかし、私はできません。 誰かが私が間違っている場所を教えてもらえますか?

(もちろん、実際のXMLは、はるかに長く複雑であるが、私はこの部分が動作していすることができるよ場合は、おそらく私は残りの部分を継続することができるよ)

答えて

1

エラーが言うように、あなたがする必要がありますクラスCollaborationInfoの前に注釈@XmlRootElementを追加します。 @XmlRootElementは静的クラスAgreementRef用です。

package modules; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElementRef; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "CollaborationInfo", propOrder = { 
    "AgreementRef", 
    "ConversationId" 
}) 
public class CollaborationInfo { 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class AgreementRef { 
     @XmlAttribute 
     private String pmode; 

     public String getPmode() { 
      return pmode; 
     } 
     public void setPmode(String pmode) { 
      this.pmode = pmode; 
     } 
    } 

    @XmlElement(name = "AgreementRef") 
    protected AgreementRef AgreementRef = new AgreementRef(); 

    @XmlElement(name="ConversationId") 
    protected String ConversationId; 


    public String getPmode() { 
     return AgreementRef.getPmode(); 
    } 

    public void setPmode(String value) { 
     this.AgreementRef.setPmode(value); 
    } 


    public String getConversationId() { 
     return ConversationId; 
    } 
    public void setConversationId(String value) { 
     this.ConversationId = value; 
    } 
} 
+0

私はそれがとても簡単だとは思わない。私はテスト中にそれをしたと確信していた、おそらくいくつかの他のコードが壊れていたし、私はちょうど同じを再度適用することを忘れてしまった。ありがとうございます! – Nihvel

関連する問題