JAXB - 如果设置了另一个可选属性,是否可以设置可选属性?

时间:2012-12-17 17:21:09

标签: java xml jaxb

如果设置了可选属性,那么我希望突然需要另一个属性。此外,这意味着只有在设置了上一个属性时才能设置梯形图可选属性。

名称是必需的

状态是可选的,但如果设置,则还需要指定国家/地区。

只有在提供状态时才设置国家/地区。

<field name="myField" status="citizen" country="England"/>

有效的xml标记也可以是status和country为null(因此不是unmarshalled):

<field name="myField" />

如果我将可选字段放在自己的xml标记中可能会更容易?也许是这样的:

<field name="myField">
     <option status="citizen" country="England"/>
</field>

虽然第一种方式可行吗?

1 个答案:

答案 0 :(得分:1)

您可以使用beforeMarshal事件并执行以下操作:

字段

如果您不希望在您的域类中使用beforeMarshal方法,则可以实施Marshaller.Listener并在Marshaller上设置(请参阅:http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/Marshaller.Listener.html)。

package forum13918891;

import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Field {

    @XmlAttribute
    String name;

    @XmlAttribute
    String status;

    @XmlAttribute
    String country;

    private void beforeMarshal(Marshaller marshaller) {
        if(country != null && status == null) {
            throw new RuntimeException("country was set but status was not");
        }
    }

}

<强>演示

下面是一些演示代码,以证明一切正常。

package forum13918891;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Field.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Field field = new Field();
        field.name = "myField";
        marshaller.marshal(field, System.out);

        field.status = "citizen";
        field.country = "England";
        marshaller.marshal(field, System.out);

        field.status = null;
        marshaller.marshal(field, System.out);
    }

}

<强>输出

以下是运行演示代码的输出。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<field name="myField"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<field name="myField" status="citizen" country="England"/>
Exception in thread "main" java.lang.IllegalStateException: java.lang.reflect.InvocationTargetException
    at com.sun.xml.bind.v2.runtime.XMLSerializer.fireMarshalEvent(XMLSerializer.java:758)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.fireBeforeMarshalEvents(XMLSerializer.java:743)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:491)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
    at forum13918891.Demo.main(Demo.java:21)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.fireMarshalEvent(XMLSerializer.java:755)
    ... 6 more
Caused by: java.lang.RuntimeException: country was set but status was not
    at forum13918891.Field.beforeMarshal(Field.java:21)
    ... 11 more