2011-10-08 58 views
1

我有一个XSD计划,但我不能改变计划如何配置Jaxb忽略使用属性?

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

的Java类的代作品,但我想忽略属性利用=“必需的”。有没有办法忽略这一点?

我想要得到这样的结果,当我元帅。

<protectionPoint zbpName="Protection Point - 0"> 

但在那一刻我得到这样的结果......。

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

这是因为生成的Clasess有这个Annotation。

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

但应该是这样的......

@XmlAttribute(name = "zbpStation") 

感谢您的帮助;-)

回答

0

我认为你可以只是正确标注创建自己的类,并用它来代替旧例如:

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; 
    } 
} 

实际上,这取决于您需要访问什么样的目标类。

+0

这个“使用”并不容易,因为JAXB类应该放在一个包中。我会说,需要在生成Java类之后进行更正。 –

+0

你不能在运行时删除注释,所以它必须在编译前手动完成 –

+0

但是我必须为许多类执行此操作,所以对我来说这不是一个真正的选择...... 谢谢。 – user985203

2

所以你想required="false"但不能改变架构?您可以使用JAXB2-Basics Annotate Plugin版本0.6.3及更高版本来实现此目的。定制将如下所示:

<?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> 

0.6.3版本尚未发布。快照可用here

+0

谢谢你的帖子非常有帮助。 – user985203