1

XJC を介して Java コードを作成した XSD がいくつかあります。XJC が提供する「ダイレクト マッピング」POJO を介して多くの情報を取得できます。残りのほとんどは、JAXBElements を介して取得できます。ただし、それらに話しかける方法がわからない要素がいくつかあります。つまり、'transaction/description' 親の下の 'cost' 要素です。

<transaction>
  <aid-type code="1995-09-25"/>
  <flow-type code="10">ODA</flow-type>
  <provider-org ref="DE-1">BMZ</provider-org>
  <value currency="EUR" value-date="1995-09-25">2070227</value>
  <transaction-type code="D">Disbursement</transaction-type>
  <description type="1" xml:lang="en">
    <costs currency="EUR" type="Personnel costs">1060135</costs>
    <costs currency="EUR" type="Material costs">665117</costs>
    <costs currency="EUR" type="Other costs">344975</costs>
  </description>
</transaction>

ご覧のとおり、Transaction.java には「description」要素が含まれており、それを JAXBElement.class にマップしています。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"valueOrDescriptionOrTransactionType"
})
@XmlRootElement(name = "transaction")
public class Transaction {

@XmlElementRefs({
    @XmlElementRef(name = "transaction-date", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "value", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "disbursement-channel", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "receiver-org", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "transaction-type", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "description", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "tied-status", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "aid-type", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "finance-type", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "provider-org", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "flow-type", type = JAXBElement.class, required = false)
})
@XmlAnyElement(lax = true)
protected List<Object> valueOrDescriptionOrTransactionType;
@XmlAttribute(name = "ref")
protected String ref;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();

Transaction.java の上で、スキーマ フラグメントは次のように述べています。

...
&lt;element name="description" type="{}textType"/>
...

したがって、「説明」のタイプは JAXBElement<TextType> である必要があります。TextType.java は次のようになります。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {"content"})
public class TextType {

@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
@XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace")
protected String lang;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();

トランザクションから情報を取得するために、トランザクション オブジェクトを作成し、その内容を取得します。

 List<Object> listOfTransactionContents = transaction.getValueOrDescriptionOrTransactionType();

これにより、JAXBElement オブジェクトを探すリストが表示されます。

for (Object obj : listOfTransactionContents)
{
    JAXBElement<TextType> jaxbElementTextType = (JAXBElement<TextType>) obj;
    TextType textType = jaxbElementTextType.getValue();
    List<Object> listOftTextTypes = textType.getContent();
    ....

しかし、ここで「コスト」要素の内容を取得する方法がわからないという問題があります。TextType.java の getContent() メソッドの上には、次のように書かれています。

 * Objects of the following type(s) are allowed in the list
 * {@link Element }
 * {@link String }
 * {@link Object }

「説明」の親の下に複数の要素が存在する可能性があるため、「コスト」要素の内容は何らかのリストに格納する必要があります。

4

2 に答える 2