开发者

Ignore parent element in XML schema

I have an xml file that looks like this:

<RootItem>
  <Items>
    <Item />
    <Item />
    <Item />
  </Items>
  <Values>
    <Value />
    <Value />
    <Value />
  </Values>
  <AnotherItem />
</RootItem>

I'm using Trang to translate this into a .xsd schema, and using xjc to translate the schema into annotated Java classes (that work smoothly with jaxB to marshal and unmarshall my documents) My only problem is that xjc gives me these classes:

  • RootItem.java
  • Items.java
  • Item.java
  • Values.java
  • Value.java
  • ObjectFactory.java (required by JaxB)

I don't want a "Items" or a "Values" class. How do I format my schema to tell it to ignore the parent element and just make a "List items" object in my RootItem class?

What I want:

  • RootItem.java
  • Item.java
  • Value.java
  • ObjectFactory.java (required by JaxB)

Thanks!

Edit: Here's the schema generated by Trang:

<xs开发者_运维问答:element name="RootItem">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="Items"/>
      <xs:element ref="Values"/>
    </xs:sequence>
    <xs:element name="AnotherItem" use="required" type="xs:NCName"/>
  </xs:complexType>
</xs:element>

<xs:element name="Items">
  <xs:complexType>
    <xs:sequence>
      <xs:element maxOccurs="unbounded" ref="Item"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="Values">
  <xs:complexType>
    <xs:sequence>
      <xs:element maxOccurs="unbounded" ref="Value"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>


You can always start from Java classes. You can use @XmlElementWrapper to get the grouping elements "Items" and "Values".

Note: JAXB does not require the object factory. JAXB can leverage metadata on a class like ObjectFactory that is annotated with @XmlRegistry.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜