36.3. 바인딩되지 않은 특성 사용

36.3.1. 개요

XML 스키마에는 복잡한 형식 정의에서 임의의 속성에 대한 자리 표시자를 남겨 둘 수 있는 메커니즘이 있습니다. 이 메커니즘을 사용하여 모든 특성이 있을 수 있는 복잡한 유형을 정의할 수 있습니다. 예를 들어 세 가지 속성을 지정하지 않고 <robot name="epsilon" />, <robot age="10000" /> 또는 <robot type="weevil" /> 요소를 정의하는 유형을 생성할 수 있습니다. 이는 데이터의 유연성이 필요할 때 특히 유용할 수 있습니다.

36.3.2. XML 스키마로 정의

선언되지 않은 특성은 anyAttribute 요소를 사용하여 XML 스키마에서 정의됩니다. 특성 요소를 사용할 수 있는 위치에서 사용할 수 있습니다.It can be used wherever an attribute element can be used. anyAttribute 요소에는 예 36.7. “Undeclared Attribute를 사용하는 복합 유형” 과 같이 속성이 없습니다.

예 36.7. Undeclared Attribute를 사용하는 복합 유형

<complexType name="arbitter">
  <sequence>
    <element name="name" type="xsd:string" />
    <element name="rate" type="xsd:float" />
  </sequence>
  <anyAttribute />
</complexType>

정의된 유형인 arbitter 에는 두 개의 요소가 있으며 모든 유형의 하나의 특성을 가질 수 있습니다. 예 36.8. “와일드 카드 특성을 사용하여 정의된 요소의 예” 에 표시된 세 가지 요소는 모두 복잡한 유형 중재에서 생성될 수 있습니다.

예 36.8. 와일드 카드 특성을 사용하여 정의된 요소의 예

<officer rank="12"><name>...</name><rate>...</rate></officer>
<lawyer type="divorce"><name>...</name><rate>...</rate></lawyer>
<judge><name>...</name><rate>...</rate></judge>

36.3.3. Java로의 매핑

anyAttribute 요소를 포함하는 복잡한 형식이 Java에 매핑되면 코드 생성기는 otherAttributes 라는 멤버를 생성된 클래스에 추가합니다. otherAttributesjava.util.Map<QName, String > 유형이며 맵의 라이브 인스턴스를 반환하는 getter 메서드가 있습니다. getter에서 반환된 맵이 실시간이므로 맵에 대한 수정 사항이 자동으로 적용됩니다. 예 36.9. “Undeclared Attribute를 사용하여 Complex Type의 클래스” 예 36.7. “Undeclared Attribute를 사용하는 복합 유형” 에 정의된 복잡한 유형에 대해 생성된 클래스를 보여줍니다.

예 36.9. Undeclared Attribute를 사용하여 Complex Type의 클래스

public class Arbitter {

    @XmlElement(required = true)
    protected String name;
    protected float rate;

    @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>();

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

    public float getRate() {
        return rate;
    }

    public void setRate(float value) {
        this.rate = value;
    }

    public Map<QName, String> getOtherAttributes() { return otherAttributes; }

}

36.3.4. 선언되지 않은 속성으로 작업

생성된 클래스의 otherAttributes 멤버는 Map 오브젝트로 채워집니다. 맵은 QNames 를 사용하여 키가 지정됩니다. 맵이 표시되면 오브젝트에 설정된 모든 속성에 액세스하고 오브젝트에 대한 새 특성을 설정할 수 있습니다.

예 36.10. “Undeclared Attributes로 작업” 선언되지 않은 특성을 사용하기 위한 샘플 코드를 보여줍니다.

예 36.10. Undeclared Attributes로 작업

Arbitter judge = new Arbitter();
Map<QName, String> otherAtts = judge.getOtherAttributes();

QName at1 = new QName("test.apache.org", "house");
QName at2 = new QName("test.apache.org", "veteran");

otherAtts.put(at1, "Cape");
otherAtts.put(at2, "false");

String vetStatus = otherAtts.get(at2);

예 36.10. “Undeclared Attributes로 작업” 의 코드는 다음을 수행합니다.

선언되지 않은 속성이 포함된 맵을 가져옵니다.

특성을 사용할 QNames를 생성합니다.

속성 값을 맵으로 설정합니다.

특성 중 하나에 대한 값을 검색합니다.