ようやく自分の問題の 1 つを絞り込んだと思います。Moxy実装でJaxbを使用しています。バインディング ファイルで Xpath 表記を使用しています。望ましい結果が得られません。
元の jaxb 生成クラスは大きくネストされているため、テストのために、コードを以下の Condition.java にスリム化しました。
条件.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "condition", propOrder = {
"diagnosisPriority",
"problemDate",
"problemType",
"problemName",
"problemCode",
"ageAtOnset",
"problemStatus",
"comment"
})
public class Condition {
protected BigInteger diagnosisPriority;
protected IvlTs problemDate;
protected Cd problemType;
@XmlElement(required = true)
protected Object problemName;
protected Cd problemCode;
protected BigInteger ageAtOnset;
protected Ce problemStatus;
protected List<Comment> comment;
//ommitted getters and setters
作成したクラス: conditionConnect.java
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class conditionConnect {
private Condition connectX;
public Condition getconditionConnect() {
return connectX;
}
public void setconditionConnect(Condition connectX) {
this.connectX = connectX;
}
}
最初のテストは、オブジェクト モデルを作成し、それを xml にマーシャリングすることでした。これは、以下のコードで正常に実行されました。
public static void main(String[] args) {
try {
int AgeInt = 36;
int DiagnoseInt = 5;
Condition InstCon = new Condition();
Cd myProblem = new Cd();
InstCon.setDiagnosisPriority(BigInteger.valueOf(DiagnoseInt));
InstCon.setProblemType(myProblem);
InstCon.setProblemName("I have Asthma");
InstCon.setAgeAtOnset(BigInteger.valueOf(AgeInt));
myProblem.setCode("1223343");
myProblem.setCodeSystem("23433.23232.23232");
myProblem.setDisplayName("Asthma");
myProblem.setCodeSystemName("ICD-9");
JAXBContext jc1 = JAXBContext.newInstance(conditionConnect.class);
Marshaller marshaller1 = jc1.createMarshaller();
marshaller1.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
conditionConnect conVar = new conditionConnect();
conVar.setconditionConnect(InstCon);
marshaller1.marshal(conVar, System.out);
出力はそうです(成功!):
<conditionConnect>
<diagnosisPriority>5</ns0:diagnosisPriority>
<problemType code="1223343" displayName="Asthma" codeSystem="23433.23232.23232" codeSystemName="ICD-9"/>
<problemName>I have Asthma</ns0:problemName>
<ageAtOnset>36</ageAtOnset>
</conditionConnect>
xml 文字列/ファイルを介してデータを受け取るため、バインディング ファイルを使用することにしました。Condition クラスの抜粋は次のとおりです。
problem.xml - データ入力
<PROBLEM_MODULE>
<ID>91</ID>
<PR_ID>124</PR_ID>
<PROBLEM_TYPE>T</PROBLEM_TYPE>
<PROBLEM_NAME>Asthma</PROBLEM_NAME>
<PROBLEM_CODE>244.9</PROBLEM_CODE>
<PATIENT_AWARENESS>N</PATIENT_AWARENESS>
<TREATING_PROVIDER_ID>23456</TREATING_PROVIDER_ID>
<PROBLEM_CS>ICD9</PCM_PROBLEM_CS>
</PROBLEM_MODULE>
私のバインディング ファイル ( conditionsBinding.xml )
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="hl7.astm.greenccd.org"
xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Condition" >
<xml-root-element name="PROBLEM_MODULE" />
<xml-type prop-order="diagnosisPriority problemDate problemType problemName problemCode ageAtOnset problemStatus comment"/>
<java-attributes>
<xml-element java-attribute="diagnosisPriority" xml-path="ID/text()" />
<xml-element java-attribute="problemDate" />
<xml-element java-attribute="problemType" name="PROBLEM_TYPE" type="Cd"/>
<xml-element java-attribute="problemName" />
<xml-element java-attribute="problemCode" />
<xml-element java-attribute="ageAtOnset" xml-path="PCM_TREATING_PROVIDER_ID/text()" />
<xml-element java-attribute="problemStatus" />
<xml-element java-attribute="comment" />
</java-attributes>
</java-type>
<java-type name="Cd">
<xml-type prop-order="code codeSystem displayName codeSystemName"/>
<java-attributes>
<xml-attribute java-attribute="code" xml-path="PR_ID/text()"/>
<xml-attribute java-attribute="codeSystem" xml-path="PROBLEM_CODE/text()"/>
<xml-attribute java-attribute="displayName" xml-path="PROBLEM_NAME/text()"/>
<xml-attribute java-attribute="codeSystemName" xml-path="PCM_PROBLEM_CS/text()"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
バインディングと xml 入力を含むメイン コード:
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/conditionsBinding.xml"));
JAXBContext jc = JAXBContext.newInstance(new Class[] {Condition.class, Cd.class}, properties);
Unmarshaller u = jc.createUnmarshaller();
Condition conditionInput = (Condition) u.unmarshal(
new File("src/conditions/exec/problems.xml"));
//Marshall Code
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, new File("src/conditions/exec/binding.xml"));
JAXBContext resultJC = JAXBContext.newInstance(new Class[] {Condition.class}, properties);
Marshaller resultMarshaller = resultJC.createMarshaller();
resultMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
resultMarshaller.marshal(conditionInput, System.out);
上記のメイン コードからの出力:
<Condition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<diagnosisPriority>91</diagnosisPriority>
<problemType/>
<ageAtOnset>23456</ageAtOnset>
</Condition>
問題: バインディングを行うとき、タグ
<problemType/>
空になるので、Cd を problemType にリンクしようとしています。したがって、problemType からの xml 出力は次のようになります。
<problemType code="1223343" displayName="Asthma" codeSystem="23433.23232.23232" codeSystemName="ICD-9"/>
バインディング ファイルに何が欠けているか教えてください。
編集: binding.xml ファイル。このファイルを使用して、xml 要素名を Java オブジェクトの変数名にマーシャリングします。
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="hl7.astm.greenccd.org"
xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Condition" xml-accessor-type="FIELD">
<xml-root-element name="Condition"/>
</java-type>
<java-type name="Cd" xml-accessor-type="FIELD">
<xml-root-element name="problemType"/>
</java-type>
</java-types>
</xml-bindings>
注: binding.xml を使用せずにコードをテストしたところ、異なる要素名で同じ結果が得られました。binding.xml のない Main.java コードは次のとおりです。
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/conditionsBinding.xml"));
JAXBContext jc = JAXBContext.newInstance(new Class[] {Condition.class, Cd.class}, properties);
// create an Unmarshaller
Unmarshaller u = jc.createUnmarshaller();
Condition conditionInput = (Condition) u.unmarshal(
new File("src/conditions/exec/problems.xml"));
Marshaller resultMarshaller = jc.createMarshaller();
resultMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
resultMarshaller.marshal(conditionInput, System.out);
} catch (JAXBException je) {
je.printStackTrace();
}
}
binding.xml ファイルなしの出力:
<?xml version="1.0" encoding="UTF-8"?>
<PROBLEM_MODULE>
<ID>91</ID>
<PROBLEM_TYPE/>
<TREATING_PROVIDER_ID>23456</TREATING_PROVIDER_ID>
</PROBLEM_MODULE>
Java クラス/フィールド名の problems.xml ファイルへのマッピングは次のとおりです。
<PROBLEM_MODULE>
<ID>91</ID> /* maps to class Condition: diagnosisPriority */
<PR_ID>124</PR_ID> /* maps to class Cd: code */
<PROBLEM_TYPE>T</PROBLEM_TYPE> /* class Condition: problemType - problemType is of type Cd.java - Cd.java is a list of attributes only
<PROBLEM_NAME>Asthma</PROBLEM_NAME> /* maps to class Cd: displayName*/
<PROBLEM_CODE>244.9</PROBLEM_CODE>/* maps to class Cd: codeSystem*/
<PATIENT_AWARENESS>N</PATIENT_AWARENESS>
<TREATING_PROVIDER_ID>23456</TREATING_PROVIDER_ID> /* maps to Condition: ageAtOnset */
<PROBLEM_CS>ICD9</PCM_PROBLEM_CS> /* maps to class Cd: codeSystemName*/
</PROBLEM_MODULE>
problems.xml ファイルの追加メモ:
<PROBLEM_TYPE>T</PROBLEM_TYPE> /* class Condition: problemType - problemType is of type Cd.java - Cd.java is a list of attributes only
私の conditionsBinding.xml ファイルでは、Problem_Type を次のようにコーディングしています。
<xml-element java-attribute="problemType" name="PROBLEM_TYPE" type="Cd"/>
これを行った理由は、Problem_Type にルート要素または name="some_field" がないためです。最初に conditionsBinding.xml で試してみました。
<xml-element java-attribute="problemType" type="Cd"/>
私がそれをしたとき、problemType のコード行を取得できなかったので、おそらくそれが私の問題である可能性があることをテストするために name="some_field" を追加しました。私はmoxy wikiの例に従っていますが、明らかに欠けているものがありますが、それを特定することはできません.
追加の編集:
以下の回答で conditionsBinding.xml を変更した後、同じ xml 出力を得ることができました。ただし、problemType は属性のリストである必要があるため、コードを次のように変更しました: conditionsBinding.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="hl7.astm.greenccd.org" xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Condition">
<xml-root-element name="PROBLEM_MODULE" />
<xml-type
prop-order="diagnosisPriority problemDate problemType problemName problemCode ageAtOnset problemStatus comment" />
<java-attributes>
<xml-element java-attribute="diagnosisPriority"
xml-path="ID/text()" />
<xml-element java-attribute="problemDate" />
<xml-element java-attribute="problemType" name="PROBLEM_TYPE"
xml-path="." />
<xml-element java-attribute="problemName" />
<xml-element java-attribute="problemCode" />
<xml-element java-attribute="ageAtOnset" name="PCM_TREATING_PROVIDER_ID" />
<xml-element java-attribute="problemStatus" />
<xml-element java-attribute="comment" />
</java-attributes>
</java-type>
<java-type name="Cd">
<xml-type prop-order="code codeSystem displayName codeSystemName" />
<java-attributes>
<xml-attribute java-attribute="code" name="PR_ID" />
<xml-attribute java-attribute="codeSystem" name="PROBLEM_CODE" />
<xml-attribute java-attribute="displayName" name="PROBLEM_NAME" />
<xml-attribute java-attribute="codeSystemName" name="PCM_PROBLEM_CS" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
出力は次のようになりました (空の problemType タグ):
<?xml version="1.0" encoding="UTF-8"?>
<Condition>
<diagnosisPriority>91</diagnosisPriority>
<problemType />
<ageAtOnset>23456</ageAtOnset>
</Condition>
Cd.javaの抜粋
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "cd", propOrder = {
"originalText",
"qualifier"
})
public class Cd {
protected Object originalText;
protected List<Qualifier> qualifier;
@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;
別の注意として、私は後でproblemCodeに注釈を付ける必要があることを認識しています。これは、Conditions.javaのAND型のフィールドでもありますが、異なるxml要素名にマップされます。これには、conditionsBinding.xml ファイルに Cd.java の別の注釈ブロックが必要になります。(マッピングは実際のものではありませんが、次のようなものが反映されます。
疑似 conditionsBinding.xml :
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="hl7.astm.greenccd.org" xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Condition">
<xml-root-element name="PROBLEM_MODULE" />
<xml-type
prop-order="diagnosisPriority problemDate problemType problemName problemCode ageAtOnset problemStatus comment" />
<java-attributes>
<xml-element java-attribute="diagnosisPriority"
xml-path="ID/text()" />
<xml-element java-attribute="problemDate" />
<xml-element java-attribute="problemType" name="PROBLEM_TYPE"
xml-path="." />
<xml-element java-attribute="problemName" />
<xml-element java-attribute="problemCode" name="PROBLEM_CODE_PSEUDO"
xml-path="."/>
<xml-element java-attribute="ageAtOnset" name="PCM_TREATING_PROVIDER_ID" />
<xml-element java-attribute="problemStatus" />
<xml-element java-attribute="comment" />
</java-attributes>
</java-type>
<java-type name="Cd">
<xml-type prop-order="code codeSystem displayName codeSystemName" />
<java-attributes>
<xml-element java-attribute="code" name="PR_ID" />
<xml-element java-attribute="codeSystem" name="PROBLEM_CODE" />
<xml-element java-attribute="displayName" name="PROBLEM_NAME" />
<xml-element java-attribute="codeSystemName" name="PCM_PROBLEM_CS" />
</java-attributes>
</java-type>
/* java-type name = Cd will be mapped to different xml elements for problemCode */
<java-type name="Cd">
<xml-type prop-order="code codeSystem displayName codeSystemName" />
<java-attributes>
<xml-element java-attribute="code" name="PR_ID_PSEUDO" />
<xml-element java-attribute="codeSystem" name="PROBLEM_CODE_PSEUDO" />
<xml-element java-attribute="displayName" name="PROBLEM_NAME_PSEUDO" />
<xml-element java-attribute="codeSystemName" name="PCM_PROBLEM_CS_PSEUDO" />
</java-attributes>
</java-type>
</java-types>
それは私のアプローチを微調整する必要があると私に思わせています(バインディングに依存するだけではありません)。私はhttp://wiki.eclipse.org/EclipseLink/UserGuide/MOXyにあるmoxyユーザーガイドを読んでいました 。私は次のオプションを調査し、検討しました: JPA (SAX/DOM を使用 - 真ん中のマッピングに対応)、xml-join-nodes、および xml-adapter。これらのオプション (ある場合) のどれが私の問題に役立つかは完全にはわかりません。専門家のアドバイスをいただければ幸いです。