How to write my XSD in order to match the desired XML and Java format using JAXB and XJC [duplicate]

老子叫甜甜 提交于 2019-12-21 22:17:22

问题


Possible Duplicate:
How generate XMLElementWrapper annotation with xjc and customized binding

I would like to be able to process XML of this format using JAXB ...

<configuration>
  <!-- more content here -->
  <things>
    <thing>
      <name>xx1</name>
      <value>yy1</value>
    </thing>
    <thing>
      <name>xx2</name>
      <value>yy2</value>
    </thing>
  </things>
  <!-- more content here -->
</configuration>

I'd like to marshal the above XML into these Java classes (for simplicity, I left modifiers such as public, protected as well as getters/setters away):

class Configuration {
  List<Thing> things;
}

class Thing {
  String name;
  String value;
}

The relevant part of my current XSD structure roughly looks like this:

<complexType name="Configuration">
  <sequence>
    <!-- ... -->
    <element name="things" type="ns:Things" minOccurs="0" maxOccurs="unbounded"/>
    <!-- ... -->
  </sequence>
</complexType>

<complexType name="Things">
  <sequence>
    <element name="thing" type="ns:Thing" minOccurs="0" maxOccurs="unbounded"/>
  </sequence>
</complexType>

Unfortunately, XJC generates also a class for Things even if that is really unnecessary in the Java part of the processing. So my output is this:

class Configuration {
  Things things;
}

class Things {
  List<Thing> thing;
}

class Thing {
  String name;
  String value;
}

Is there any way I can tell XJC to avoid generating this unnecessary class? Or is there any way I can re-phrase my XSD in order to avoid that generation? Both options would be fine with me.

In fact, I guess I would need to generate the @XmlElementWrapper annotation as documented here:

  • Mapping Java collections which contains super- and sub-types with JAXB
  • JAXB List Tag creating inner class

回答1:


A possible solution is documented in this question here:

How generate XMLElementWrapper annotation with xjc and customized binding

This XJC plugin allows for generating the following Java code, doing precisely what I needed (irrelevant annotations omitted):

class Configuration {

  @XmlElementWrapper(name = "things")
  @XmlElement(name = "thing")
  List<Thing> things;
}

class Thing {
  String name;
  String value;
}



回答2:


You must take into consideration that a complex type translates roughly to a class. So in your XSD schema "Things" complex type is not neccessary. Your XSD should look like:

    <complexType name="Configuration">
    <sequence>
        <element name="things">
        <complexType>
            <sequence>
                <element name="thing" type="ns:Thing" minOccurs="0"
                    maxOccurs="unbounded" />
            </sequence>
            </complexType>F
        </element>
    </sequence>
    <!-- ... -->

</complexType>

<complexType name="Thing">
    <sequence>
        <element name="name" type="string" />
        <element name="value" type="string" />
    </sequence>
</complexType>


来源:https://stackoverflow.com/questions/9240837/how-to-write-my-xsd-in-order-to-match-the-desired-xml-and-java-format-using-jaxb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!