2011-08-12 13 views
1

私はJaxBを使い始めていますが、Moxyの実装を使用しています。私はJaxbを使ってJava Object Modelに変換した業界標準のxsdを持っています。文字列、整数、日付のような簡単なフィールドに注釈を付ける限り、私は得ました。JAXB Moxy- xsd複合型のフィールドに注釈を付ける方法の質問

私は、4つの属性とオプションの文字列要素を持つxsd複合型の次のフィールドに注釈を付けるために正しい方向を指し示す必要があります。次のように生成されたコードのサブセットがある:

Conditions.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "condition" 
}) 
@XmlRootElement(name = "conditions") 
public class Conditions { 
protected List<Conditions.Condition> condition; 

public List<Conditions.Condition> getCondition() { 
    if (condition == null) { 
     condition = new ArrayList<Conditions.Condition>(); 
    } 
    return this.condition; 
} 

@XmlAccessorType(XmlAccessType.FIELD) 
     @XmlType(name = "", propOrder = { 
      "problemDate", 
      "problemType", 
      "problemCode", 
      "problemStatus",   
    }) 
    public static class Condition { 

     protected IvlTs problemDate; 
     //This is the field I need to annotate (problemType) 
     protected Cd problemType; 
     //The 2 below fields (problemCode, problemStatus) will also have to be annotated but I am just focusing on problemType for now 
     protected Cd problemCode; 
     protected Ce problemStatus 

public void setProblemDate(IvlTs value) { 
      this.problemDate = value; 
     } 

public void setProblemType(Cd value) { 
      this.problemType = value; 
     } 
public void setProblemCode(Cd value) { 
      this.problemCode = value; 
     } 
public void setProblemStatus(Ce value) { 
      this.problemStatus = value; 
     } 
//omitted getters 
    } 

Cd.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "cd", propOrder = { 
    "originalText", 
}) 

public class Cd { 

    protected Object originalText; 

    @XmlAttribute(name = "code") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String code; 

    @XmlAttribute(name = "displayName") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String displayName; 

    @XmlAttribute(name = "codeSystem") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String codeSystem; 

    @XmlAttribute(name = "codeSystemName") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String codeSystemName; 

    @XmlAttribute(name = "nullFlavor") 
    protected NullFlavorType nullFlavor; 

//ommitted getters and setters 

Cd.javaクラスは多数のために使用されますその他のクラスは、Conditions.javaクラスだけでなく、

ProblemTypeが4つの属性と1つのオプションの要素を持つConditions.javaのproblemTypeにフィールドを注釈する方法を私の質問で特に説明します。

Cd.javaに直接注釈を付けることはできません。xmlの入力は、実装しているクラス(Cd.javaクラスを使用する他の8つのクラスの中から選択)によって異なります。次のようにJAXBでConditions.java problemTypeのためのXML入力自動生成された上記の既存の注釈がある:私は私の質問を明確にする必要がどこに助言

<PROBLEM_MODULE> 
     <code>24434</code> //Maps to protected String code in Cd.java; 
     <codeName>ICD-9</codeName> //Maps to protected String codeSystem in Cd.java; 
     <display>Asthma</display> //Maps to protected String displayName in Cd.java; 
     <codeSystem>2.564.34343.222</codeSystem> // Maps to protected String codeSystemName in Cd.java; 
</PROBLEM_MODULE> 

してください。最終的に私はこれを手助けするためのリソースやチュートリアルを要求しています。私は通りではない別のプロジェクトでそれをテストしたよう

*

** *** *UPDATE* ** ** * ブレーズのソリューションは、完全に働きました複合体。したがって、この方法は正しいですが、メタデータファイルに間違っていることがあります。上記のConditions.javaファイルを更新しました。メタデータファイルを実装する方法に影響を与える可能性のある詳細は除外されています。

マイoxm.xmlファイル

<?xml version="1.0" encoding="UTF-8"?> 
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    package-name="conditions.exec" 
    xml-mapping-metadata-complete="true"> 
    <java-types> 
     <java-type name="Conditions" xml-accessor-type="FIELD"> 
      <xml-root-element name="PROBLEM_MODULE"/> 
     </java-type> 
     <java-type name="Cd" xml-accessor-type="FIELD"> 
      <java-attributes> 
      <xml-type prop-order="code codeSystem displayName codeSystemName"/> 
       <xml-element java-attribute="codeSystem" name="codeName"/> 
       <xml-element java-attribute="displayName" name="display"/> 
       <xml-element java-attribute="codeSystemName" name="codeSystem"/> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

* メインクラス *

public static void main(String[] args) { 

     try { 
     Map<String, Object> properties = new HashMap<String, Object>(1); 
     properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, new File("src/conditions/exec/oxm.xml")); 
     JAXBContext jc = JAXBContext.newInstance(new Class[] {Conditions.class,Cd.class}, properties); 

      // create an Unmarshaller 
      Unmarshaller u = jc.createUnmarshaller(); 
      conditions.exec.Conditions InventoryInput = (conditions.exec.Conditions) u.unmarshal( 
         new File("src/conditions/exec/problems.xml")); //input file 



      // create a Marshaller and marshal to a file 



     Marshaller resultMarshaller = jc.createMarshaller(); 
     resultMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     resultMarshaller.marshal(InventoryInput, System.out); 

     } catch (JAXBException je) { 
      je.printStackTrace(); 
     } 

答えて

1

あなたのクラスに第2のマッピングを適用するためにEclipseLink JAXB (MOXy)の外部結合ファイルを活用することができます

oxm.xml

私がこのファイルに設定したものはxml-mapping-metadata-complete="true"です。この設定はMOXyに注釈を完全に無視し、このファイルを使用するように指示します。デフォルトでは、注釈を補うためにOXMファイルが使用されます。

<?xml version="1.0"?> 
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    package-name="forum7043389" 
    xml-mapping-metadata-complete="true"> 
    <java-types> 
     <java-type name="Root2"> 
      <xml-root-element/> 
     </java-type> 
     <java-type name="Cd"> 
      <xml-type prop-order="code codeSystem displayName codeSystemName"/> 
      <java-attributes> 
       <xml-element java-attribute="codeSystem" name="codeName"/> 
       <xml-element java-attribute="displayName" name="display"/> 
       <xml-element java-attribute="codeSystemName" name="codeSystem"/> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

デモ

oxm.xmlファイルがJAXBContextを作成するためのプロパティとして渡されます。例ではjc1の下のクラスに作成され、jc2はクラスで作成され、oxm.xml

package forum7043389; 

import java.util.HashMap; 
import java.util.Map; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 

import org.eclipse.persistence.jaxb.JAXBContextFactory; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     Cd cd = new Cd(); 
     cd.setCode("24434"); 
     cd.setCodeSystem("ICD-9"); 
     cd.setDisplayName("Asthma"); 
     cd.setCodeSystemName("2.564.34343.222"); 

     JAXBContext jc1 = JAXBContext.newInstance(Root1.class); 
     Marshaller marshaller1 = jc1.createMarshaller(); 
     marshaller1.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     Root1 root1 = new Root1(); 
     root1.setCd(cd); 
     marshaller1.marshal(root1, System.out); 

     Map<String, Object> properties = new HashMap<String, Object>(1); 
     properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum7043389/oxm.xml"); 
     JAXBContext jc2 = JAXBContext.newInstance(new Class[] {Root2.class}, properties); 
     Marshaller marshaller2 = jc2.createMarshaller(); 
     marshaller2.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     Root2 root2 = new Root2(); 
     root2.setCd(cd); 
     marshaller2.marshal(root2, System.out); 
    } 

} 

出力

次はデモを実行しているから出力されます:

<?xml version="1.0" encoding="UTF-8"?> 
<root1> 
    <cd code="24434" displayName="Asthma" codeSystem="ICD-9" codeSystemName="2.564.34343.222"/> 
</root1> 
<?xml version="1.0" encoding="UTF-8"?> 
<root2> 
    <cd> 
     <code>24434</code> 
     <codeName>ICD-9</codeName> 
     <display>Asthma</display> 
     <codeSystem>2.564.34343.222</codeSystem> 
    </cd> 
</root2> 

Cd

package forum7043389; 

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "cd", propOrder = {"originalText",}) 
public class Cd { 

    protected Object originalText; 

    @XmlAttribute(name = "code") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String code; 

    @XmlAttribute(name = "displayName") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String displayName; 

    @XmlAttribute(name = "codeSystem") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String codeSystem; 

    @XmlAttribute(name = "codeSystemName") 
    @XmlSchemaType(name = "anySimpleType") 
    protected String codeSystemName; 

    @XmlAttribute(name = "nullFlavor") 
    protected NullFlavorType nullFlavor; 

    public Object getOriginalText() { 
     return originalText; 
    } 

    public void setOriginalText(Object originalText) { 
     this.originalText = originalText; 
    } 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getDisplayName() { 
     return displayName; 
    } 

    public void setDisplayName(String displayName) { 
     this.displayName = displayName; 
    } 

    public String getCodeSystem() { 
     return codeSystem; 
    } 

    public void setCodeSystem(String codeSystem) { 
     this.codeSystem = codeSystem; 
    } 

    public String getCodeSystemName() { 
     return codeSystemName; 
    } 

    public void setCodeSystemName(String codeSystemName) { 
     this.codeSystemName = codeSystemName; 
    } 

    public NullFlavorType getNullFlavor() { 
     return nullFlavor; 
    } 

    public void setNullFlavor(NullFlavorType nullFlavor) { 
     this.nullFlavor = nullFlavor; 
    } 

} 

ROOT1

package forum7043389; 

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class Root1 { 

    private Cd cd; 

    public Cd getCd() { 
     return cd; 
    } 

    public void setCd(Cd cd) { 
     this.cd = cd; 
    } 

} 

Root2

package forum7043389; 

public class Root2 { 

    private Cd cd; 

    public Cd getCd() { 
     return cd; 
    } 

    public void setCd(Cd cd) { 
     this.cd = cd; 
    } 

} 

詳細情報

+0

私はいくつかのコメントに上記の私のポストを更新しました。私はこれを別のプロジェクト(あまり複雑でないオブジェクトモデル)で動作させることができましたが、上記のオブジェクトモデル(Conditions.java、Cd.javaなど)で動作するようにはできませんでした。私のoxm.xmlファイルで間違っていることがあります。名前の衝突エラーが発生しています。 JAXBが生成したConditions.javaを更新しました。これは、重要だった可能性のある詳細を省きました。私もoxm.xmlファイルを追加しました。私はあなたの意見と提案を感謝します。 – Hjones

+0

衝突エラーは表示されなくなりましたが、私のoxmファイルを正しく取得しようとしています。 – Hjones

関連する問題