3

EclipseLink MOXy 2.4.1 バージョンで注釈を@XmlElement.required使用してフラグを実装する方法は?@XmlPath

4

1 に答える 1

2

@XmlElement(required=true)注釈とともに を使用し@XmlPathて、リーフ要素が必須であることを指定できます。

お客様

以下は、2 つのフィールドがマップされたドメイン モデルのサンプル@XmlPathです@XmlElement(required=true)

package forum13854920;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlPath("personal-info/first-name/text()")
    private String firstName;

    @XmlPath("personal-info/last-name/text()")
    @XmlElement(required=true)
    private String lastName;

}

jaxb.properties

MOXy を JAXB プロバイダーとして使用するにはjaxb.properties、次のエントリを使用して、ドメイン モデルと同じパッケージで呼び出されるファイルを含める必要があります。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

XML スキーマ

以下は、ドメイン モデルに対応する XML スキーマです。last-name要素に がないことに注意してくださいminOccurs="0"

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="customer">
      <xsd:sequence>
         <xsd:element name="personal-info" minOccurs="0">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:element name="first-name" type="xsd:string" minOccurs="0"/>
                  <xsd:element name="last-name" type="xsd:string"/>
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

デモ

次のデモ コードを使用して、XML スキーマを生成できます。

package forum13854920;

import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }

        });
    }

}

現在、EclipseLink JAXB (MOXy)には、パスの他のセグメントのアノテーションにrequiredプロパティに相当するものはありません。@XmlElementこの動作に関心がある場合は、以下のリンクを使用して機能強化のリクエストを入力してください。

于 2012-12-13T11:45:00.333 に答える