Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize JAXB 2.0 generated method names from a xsd:choice element

Tags:

java

xml

jaxb

I'm trying to figure out how to customize a method name that's being generated by JAXB 2.1.12. I have an epic XML schema (that is not in my control), and it contains a group with a choice element that contains about 200 other elements. So when I'm generating the JAXB classes, the get() method that is created is rather unhelpful - it ends up being something like "getElement1AndElement2AndElement3()".

I'd like to rename the get() method to be something better, but I can't figure out exactly how to do that. I know in the tutorial it describes customizing class names by using on an element.

I found a really old article (http://onjava.com/pub/a/onjava/2003/12/10/jaxb.html?page=2) that describes attaching a "name" attribute to a element, which seems to be what I need to do. Unfortunately, I think that article refers to a 1.x JAXB version, and the name attribute is no longer valid in a choice element as of JAXB 2.0.

So does anybody know how I can customize the method name that gets generated? Please let me know if examples or any other information would be helpful, or if what I'm trying to do just isn't possible.

Thanks!

like image 231
Mark D Avatar asked Dec 14 '25 12:12

Mark D


1 Answers

I've tried with the example exposed in the link you provide and the following binding.xjb works:

<jxb:bindings version="1.0"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings>
      <jxb:bindings schemaLocation="po4.xsd" node="/xs:schema/xs:element[@name='Widgets']//xs:complexType//xs:choice">
            <jxb:property name="Shapes"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

I use JAXB 2.1.12 as well.

I hope it helps.

like image 159
rochb Avatar answered Dec 16 '25 08:12

rochb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!