users@jaxb.java.net

Re: custom binding for single element XML

From: Joe Fialli <Joseph.Fialli_at_sun.com>
Date: Fri, 27 Jun 2003 13:08:35 -0400

Steve Maring wrote:

>I've confirmed, with RI 1.0.1, that I can bind a "Global Element" [spec
>ref 6.7.3.4], which appears to be defined as an Element containing a
>complexType, to a custom class name.
>
It does not mention complexType in the spec, the example is only for a
complexType,
but the customization applies for both.

> The default binding behavior is for
>an Element containing a simpleType to be bound to a property of the
>containing complexType.
>
an Element in a containing complexType is considered a "local element"

> However, there seems to be special handling when
>a simpleType Element is actually the root element and thus has no
>complexType container. This behavior is to bind the Element name as the
>interface (class) name and allow the modification of it's value as a
>property called Value.
>
>I guess I have two questions on this end:
>
>1) Can a simpleType Element contained in a complexType be forced to bind
>to a class containing a Value property instead of being bound to a
>property of the containing complexType?
>
I was curious what your need is for such a customization
of binding a child element in a more complex manner.

Below are some circumstances where JAXB can not avoid generating
an element interface for a child element. We prefer to avoid the
extra level of direction unless it is necessary.

A child element of a complex type does need to have a nested element
interface generated for it for the following cases:
-complex type has mixed content.
-child element occurs within a repeating occurance model group

Instances of the element interface are added to Collection property
representing the above two XML
Schema concepts.See Section 5.9.3.1 for an example.

> Can I also bind an XML attribute
>to a class containing a Value property?
>
No.

>
>2) How do I customize the class name for the case where a simpleType
>Element is the root Element?
>
>
Add customization <jaxb:class name="someName"/>. It should work.
Try inline customizations first if you are having difficulties with
external binding declaraion.

-Joe

>
>Cheers.
>Steve Maring
>
>
>
>
>>Steve Maring wrote:
>>
>>
>>
>>> I have an XML file like this:
>>>
>>><?xml version="1.0" encoding="UTF-8"?>
>>><MY_ELEMENT>text</MY_ELEMENT>
>>>
>>>
>>>I'd like to have that bound to a classname of MyElement with a single
>>>property called Value.
>>>
>>>The XSD looks like this:
>>>
>>><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>>> <xsd:element name="MY_ELEMENT" type="xsd:string"/>
>>></xsd:schema>
>>>
>>>I've tried this but it does not work:
>>>
>>><jaxb:bindings version="1.0"
>>> xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
>>> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>>> <jaxb:bindings schemaLocation="MyElement.xsd" node="/xsd:schema">
>>> <jaxb:bindings node="//xsd:element[@name='MY_ELEMENT']">
>>> <jaxb:class name="MyElement"/>
>>> </jaxb:bindings>
>>> </jaxb:bindings>
>>></jaxb:bindings>
>>>
>>>
>>>I keep ending up with a class named MY_ELEMENT!
>>>
>>>
>>>
>>Are you using JAXB 1.0? When I run your sample against 1.0.1, I get a
>>MYELEMENT.java without the customization, and a MyElement.java with it.
>>
>>I seem to recall there were some bugs regarding the XML -> Java name
>>conversion in JAXB 1.0...
>>
>>
>>
>>>Any thoughts would be greatly appreciated. I've got the complicated XML
>>>licked!
>>>
>>>
>>>
>>Congrats - you're a better person than I am ;8-)
>>
>>--Ryan
>>
>>
>>
>>>Steve Maring
>>>Tarpon Springs, FL
>>>
>>>
>>>