The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » Metro and JAXB

Thread: customizing with JAXB to return string value

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is not answered. Helpful answers available: 2. Correct answers available: 1.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 3 - Last Post: Sep 8, 2008 10:05 PM by: Clive Brettingh...
techiesaf

Posts: 20
customizing with JAXB to return string value
Posted: Sep 8, 2008 10:59 AM
 
  Click to reply to this thread Reply

Hi
In my schema i have an Address elements like this:
<xsd:element name="Address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="StreetAddress"/>
<xsd:element name="City" minOccurs="0"/>
<xsd:element name="State" minOccurs="0"/>
<xsd:element name="Zip" minOccurs="0"/>
<xsd:element name="Country" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

When i use JAXB to generate the Java classes the elements StreetAddress, City etc all are translated as "Objects". I want to foce the binding to String. In my binding class i did something like this but the compiler fails with this message:

" [xjc] [ERROR] compiler was unable to honor this javaType customization. It is attached to a wrong place, or its inconsistent with other bindings."


This is what i have as part of my binding file - what am i doing wrong here?

<jaxb:bindings node=".//xsd:element[@name='Address']//xsd:element[@name='StreetAddress']">
<jaxb:javaType name="String" parseMethod="javax.xml.bind.DatatypeConverter.parseString"
printMethod="javax.xml.bind.DatatypeConverter.parseString"/>
</jaxb:bindings>

Clive Brettingh...
Re: customizing with JAXB to return string value
Posted: Sep 8, 2008 5:22 PM   in response to: techiesaf
  Click to reply to this thread Reply

You are only allowed to use that customisation for simpleTypes (since
you have not given the elements types they default to anyType).

It looks like in your case the elements probably will have only string
content (intentional use of element default content model is rare) so in
this case you can just set the element content type to xsd:string and
you will get String with no need for customisation eg:
<xsd:element name="City" minOccurs="0" type="xsd:string"/>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
For additional commands, e-mail: users-help@metro.dev.java.net


techiesaf

Posts: 20
Re: customizing with JAXB to return string value
Posted: Sep 8, 2008 6:10 PM   in response to: Clive Brettingh...
 
  Click to reply to this thread Reply

Thanks for the reply - but the problem is that the schema is not under my control - its a standard we're using so I need to somehow force this binding.

Is there anyway to go about it?

Clive Brettingh...
Re: customizing with JAXB to return string value
Posted: Sep 8, 2008 10:05 PM   in response to: techiesaf
  Click to reply to this thread Reply

Someone produced *that* schema and called it a standard?

There are ways to customise complex content mapping, but they are very
involved (eg http://forums.java.net/jive/thread.jspa?messageID=296931 ),
and pointless unless there is any chance the elements will actually have
complex content (ie child elements; the names given don't make that look
likely).
Unless it is likely that the elements will have complex content in
future (in which case a mapping to a string would be weird), you can
just use you own local doctored version of the schema that sets the
types of the elements. (another alternative is to use customisations to
disable generation of the mappings for the address type and write you
own that use string, but in the end this is just ends up with
essentially the same code).

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
For additional commands, e-mail: users-help@metro.dev.java.net





 XML java.net RSS