Re: JAXB2 Basics, toString generation
- From: Erik <et57@xxxxxxxxxxx>
- Date: Fri, 11 Mar 2011 19:41:13 +0100
I can get it done with code injection (-Xinject-code cmd line option),
via a Netbeans project and an ANT build file. I add this to the xsd in
the appropriate spot:
<xs:annotation>
<xs:appinfo>
<ci:code>
// any code you can think of:
/**
public String toString() {
return name;
}
*/
</ci:code>
</xs:appinfo>
</xs:annotation>
But JAXB2 Basics advertises an -XtoString option, that is supposed to
do it neatly.
I know the links you mentioned: been there, done that.
As a matter of high probability,I think I saw ALL the (ir-)relevant
doc on this subject. Pity there is no example code. You'd say the
maker would have little trouble providing that... but alas.
..
On Fri, 11 Mar 2011 06:56:36 -0800 (PST), Lew <lew@xxxxxxxxxxxxx>
wrote:
Erik wrote:.
How can I have a toString method generated by xjc (using Jaxb2-Basics)
for class "person" that gives me his/her "name"?
Here is the schema, i want to use:
I'll add indentation and blank lines for readability.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xmlns:basic="http://jaxb2-commons.dev.java.net/basic"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
xmlns:ts="http://jaxb2-commons.dev.java.net/basic/toString"
jaxb:extensionBindingPrefixes="basic ci ts">
<xs:element name="names">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="person" form="unqualified">
<xs:complexType>
<xs:all>
<xs:element name="name" form="unqualified" type="xs:string"/>
<xs:element name="age" form="unqualified" type="xs:int"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I don't know how to get JAXB to generate a 'toString()' for you unless
I check the documentation, sorry.
I've used two approaches with JAXB-generated classes where I needed
something beyond the generator's output: modify the output or wrap it
in an adapter. The latter makes for a simpler build process.
For some reason I had a bookmark link to
http://java.net/projects/jaxb2-commons/pages/Home
which seems to indicate that others might have faced this question
before:
http://confluence.highsource.org/display/J2B/Equals+plugin
I don't remember precisely how I found that site to bookmark it. I've
been doing a lot of JAXB work and I suppose I must have googled around
for useful information.
- Follow-Ups:
- Re: JAXB2 Basics, toString generation
- From: Lew
- Re: JAXB2 Basics, toString generation
- References:
- JAXB2 Basics, toString generation
- From: Erik
- Re: JAXB2 Basics, toString generation
- From: Lew
- JAXB2 Basics, toString generation
- Prev by Date: Re: Java’s Future Lies In Mobile?
- Next by Date: Re: JAXB2 Basics, toString generation
- Previous by thread: Re: JAXB2 Basics, toString generation
- Next by thread: Re: JAXB2 Basics, toString generation
- Index(es):