开发者

How to config Jaxb to ignore the use Attribute?

开发者 https://www.devze.com 2023-04-12 03:23 出处:网络
I have a XSD scheme b开发者_StackOverflow社区ut I cannot change the Scheme  <xs:attribute name=\"zbpName\" type=\"Zbp_NC\" use=\"required\"/>

I have a XSD scheme b开发者_StackOverflow社区ut I cannot change the Scheme 

<xs:attribute name="zbpName" type="Zbp_NC" use="required"/>
<xs:attribute name="zbpType" type="ZBPTYP_CL" use="required"/

Generation of Java-Classes works, but I want to ignore the Attribute use="required”. Is there a way to ignore this?

I want to get this result when I marshal.

<protectionPoint zbpName="Protection Point - 0">

But in the moment I get this result….

<protectionPoint zbpNotes="" zbpStation="" zbpInterlockingName="" zbpType="" zbpName="Protection Point - 0">

It is because the generated Clasess have this Annotation.

@XmlAttribute(name = "zbpStation", required = true)

But the should look like this…

@XmlAttribute(name = "zbpStation")

Thanks for your help ;-)


So you want to required="false" but can't change schema? You can use JAXB2-Basics Annotate Plugin version 0.6.3 and higher to achieve this. The customization would look like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jaxb:extensionBindingPrefixes="xjc annox"
    version="2.1">

    <!-- org.example.TFreeForm @XmlRootElement -->
    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='MyType']/xs:attribute[@name='test']">
            <annox:annotate target="field">
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlAttribute" required="false"/>
            </annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

The 0.6.3 version is not released yet. A snapshot is available here.


I think you can just create your own class with correct annotations and use it instead of old one, e.g.:

public interface TestInterface {
    Integer getField();     
}

public class TestClass implements TestInterface{

    @Attribute(required = true)
    private Integer field;

    public TestClass() {
    }

    public TestClass(Integer field) {
        this.field = field;
    }

    public Integer getField() {
        return field;
    }

    public void setField(Integer field) {
        this.field = field;
    }
}

public class NewTestClass implements TestInterface{

    @Attribute
    private Integer field;

    public NewTestClass() {
    }

    public NewTestClass(Integer field) {
        this.field = field;
    }

    public Integer getField() {
        return field;
    }

    public void setField(Integer field) {
        this.field = field;
    }
}

Actually, it depends on what access you have to target class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号