Creating Pretty JAXB Classes

I have used JAXB to parse XML in the past and created my own simple classes for it. These were POJOs like

public class Foo {

    @XmlAttribute
    public String someAttribute;

    public String someElement;

    public Bar bar;
}

      

Now I want to write a tool that needs to parse data from a very complex xml structure and I would like not to write it myself. I tried using xjc to generate the classes, but they look completely different from my example above:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RulesElementType", propOrder = { "content" })
public class RulesElementType {

    @XmlElementRefs({ @XmlElementRef(name = "flavor2", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "specific", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "category2", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "flavor1", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "print-prereqs", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "prereqs2", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "rules", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "prereqs1", type = JAXBElement.class, required = false) })
    @XmlMixed
    protected List<Serializable> content;
    @XmlAttribute(name = "name")
    protected String name;
    @XmlAttribute(name = "type1")
    protected String type1;
    @XmlAttribute(name = "internal-id")
    protected String internalId;
    @XmlAttribute(name = "source")
    protected String source;
    @XmlAttribute(name = "revision-date")
    protected String revisionDate;

    /* getters and setters omitted */

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "value" })
    public static class Category2 {

        @XmlValue
        protected String value;
        @XmlAttribute(name = "name")
        protected String name;

        /* getters and setters omitted */

    }

    /* additional class definitions omitted */

}

      

To get an element named "flavor1" I need to find the "content" attribute for a JAXBElement named "flavor1", which I find terribly awkward. What I would like to do is something like:

String flavor1 = rulesElement.getFlavor1();

      

Is there a way to accomplish this with xjc or some other tool?

Edit: The complexType for RulesElementType from my xsd:

  <xs:complexType name="RulesElementType" mixed="true">
    <xs:choice maxOccurs="unbounded" minOccurs="0">
      <xs:element name="category2">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute type="xs:string" name="name" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
      <xs:element name="prereqs2">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute type="xs:string" name="name" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
      <xs:element type="xs:string" name="print-prereqs"/>
      <xs:element name="flavor2">
        <xs:complexType mixed="true">
          <xs:choice maxOccurs="unbounded" minOccurs="0">
            <xs:element type="grantType" name="grant"/>
            <xs:element type="stataddType" name="statadd"/>
            <xs:element type="textstringType" name="textstring"/>
            <xs:element type="selectType" name="select"/>
            <xs:element type="replaceType" name="replace"/>
            <xs:element type="modifyType" name="modify"/>
            <xs:element type="dropType" name="drop"/>
            <xs:element type="suggestType" name="suggest"/>
            <xs:element type="stataliasType" name="statalias"/>
          </xs:choice>
          <xs:attribute type="xs:string" name="name" use="optional"/>
        </xs:complexType>
      </xs:element>
      <xs:element type="specificType" name="specific"/>
      <xs:element type="rulesType" name="rules"/>
      <xs:element type="xs:string" name="prereqs1"/>
      <xs:element name="flavor1">
        <xs:complexType mixed="true">
          <xs:choice maxOccurs="unbounded" minOccurs="0">
            <xs:element type="grantType" name="grant"/>
            <xs:element type="stataddType" name="statadd"/>
            <xs:element type="textstringType" name="textstring"/>
            <xs:element type="selectType" name="select"/>
            <xs:element type="replaceType" name="replace"/>
            <xs:element type="modifyType" name="modify"/>
            <xs:element type="dropType" name="drop"/>
            <xs:element type="suggestType" name="suggest"/>
            <xs:element type="stataliasType" name="statalias"/>
          </xs:choice>
          <xs:attribute type="xs:string" name="name" use="optional"/>
        </xs:complexType>
      </xs:element>
    </xs:choice>
    <xs:attribute type="xs:string" name="name" use="optional"/>
    <xs:attribute type="xs:string" name="type1" use="optional"/>
    <xs:attribute type="xs:string" name="internal-id" use="optional"/>
    <xs:attribute type="xs:string" name="source" use="optional"/>
    <xs:attribute type="xs:string" name="revision-date" use="optional"/>
  </xs:complexType>

      

The XML file looks somewhat similar to this .

I am using XMLBeans to generate xsd. Since the XML file I am reading is about 680K lines, manually generating the XSD is not an option.

+3


source to share


1 answer


You can use the Simplify plugin to simplify your property content

.

Disclaimer SO: I am the author.

Please see these questions:



Generate java classes from xsd using jaxb from selection

retrieving the text value of an element

In short, if you use this plugin and customize one of the elements that go into your property content

with simplify:as-element-property

, you can get different easy-to-use element properties for each element. Hopefully this is what you mean "pretty".

+2


source







All Articles