私は JaxB を使い始めており、Moxy 実装を使用しています。Jaxb を使用して Java オブジェクト モデルに変換した業界標準の xsd があります。文字列、整数、日付などの単純なフィールドに注釈を付けることができました。
私は検索しており、4 つの属性とオプションの文字列要素を持つ xsd 複合型である次のフィールドに注釈を付けるために、正しい方向を指す必要があります。生成されたコードのサブセットは次のとおりです。
条件.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 クラスだけでなく、他の多くのクラスにも使用されます。
特に私の質問は、Conditions.java の problemType のフィールドにどのように注釈を付けるかです。ここで、problemType には 4 つの属性と 1 つのオプション要素があります。
実装しているクラス (Cd.java クラスを使用する 8 つの他のクラスの選択) によって xml 入力が異なるため、Cd.java に直接注釈を付けることはできません。上記の既存の注釈は、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>
私の質問を明確にする必要がある場所を教えてください。最終的には、これを支援するためのリソースまたはチュートリアルをリクエストしています。
** * ***更新* ** * ** * Blaise のソリューションは、それほど複雑ではない別のプロジェクトでテストしたところ、完全に機能しました。したがって、方法は正しいのですが、メタデータ ファイルに問題があります。上記の 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();
}